Connect to url in python

I am trying to connect to a url with username and password with the following code:

urllib.request.urlopen("http://username: password@......etc... ", None) 

but i get

 urllib.error.URLError: urlopen error [Errno 11003] getaddrinfo failed 

Does anyone know what?

+4
source share
3 answers

Sorry. I did not notice that you are using py3k.
See urllib.request - FancyURLopener . I personally don't know py3k very well.
Basically, you need to subclass urllib.request.FancyURLopener , override prompt_user_passwd(host, realm) , and then call YourClass.urlopen(url) .

Below for py2

This is what you want urllib2 - Basic Authentication
Below is the code from this page, in case the link will rot at some point.

 # create a password manager password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() # Add the username and password. # If we knew the realm, we could use it instead of None. top_level_url = "http://example.com/foo/" password_mgr.add_password(None, top_level_url, username, password) handler = urllib2.HTTPBasicAuthHandler(password_mgr) # create "opener" (OpenerDirector instance) opener = urllib2.build_opener(handler) # use the opener to fetch a URL opener.open(a_url) # Install the opener. # Now all calls to urllib2.urlopen use our opener. urllib2.install_opener(opener) 
+5
source

You must use urllib.request.HTTPBasicAuthHandler for HTTP authentication.

HTTP does not handle authentication with user: password@host .

+2
source

If you can install third-party libraries, then httplib2 will be easier to use and a more powerful alternative to urllib.request :

 import httplib2 h = httplib2.Http("/path/to/cache-directory") h.add_credentials(username, password) response, content = h.request(url) assert response.status == 200 
+1
source

Source: https://habr.com/ru/post/1335736/


All Articles