4

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

urllib.request.urlopen("http://username:[email protected]...", None)

but i'm getting

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

anyone know what's up?

5
  • What do you mean by URL with username and password? Do you mean a page that will return "401 Unauthorized". Commented Jan 16, 2011 at 15:57
  • yes, authentication is required to connect to this server Commented Jan 16, 2011 at 16:04
  • BTW, You need to use "@Gene" if you want me be notified because of your reply. (I saw this reply because I anticipated you don't know you need this) Commented Jan 16, 2011 at 16:07
  • @rach: What SilentGhost means is this: Go back to one of your old questions. Decide which answer helped you most. Click on the outline of a checkmark next to that answer. Repeat for all of your questions. Commented Jan 16, 2011 at 23:00
  • @rach: That's a strange error. I get "Nonnumeric port". In any case it's clear that urllib doesn't accept the username:password@hostname syntax. So, What Gene said. Commented Jan 16, 2011 at 23:18

3 Answers 3

5

I'm sorry. I didn't notice 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 is for py2

This is what you want, urllib2 - Basic Authentication
Below is the code from that page, just in case some day the link rot.

# 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)
Sign up to request clarification or add additional context in comments.

3 Comments

@SlientGhost Thank you. I didn't notice that. I've update it soon.
@rach You're welcome. But I guess you'll have to figure out how it works on py3k. It's not as straightforward as py2 is. BTW, in this case, you can omit @Gene because I'm the owner of this answer and will automatically be notified.
It is exactly as straightforward, but instead of urllib2 you use urllib.request.
2

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

HTTP doesn't handle authentication in the user:password@host way.

Comments

1

If you can install 3rd party libraries then httplib2 is both easier to use and 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.