4

How do you open https url in Python?

import urllib2

url = "https://user:[email protected]/path/
f = urllib2.urlopen(url)
print f.read()

gives:

httplib.InvalidURL: nonnumeric port: '[email protected]'
1
  • THe way to read https URL's in Python is to actually search stackoverflow before posting the same question that's already been asked. Here's the search results: stackoverflow.com/search?q=%5Bpython%5D+https Commented Dec 15, 2009 at 12:07

4 Answers 4

11

This has never failed me

import urllib2, base64
username = 'foo'
password = 'bar'
auth_encoded = base64.encodestring('%s:%s' % (username, password))[:-1]

req = urllib2.Request('https://somewebsite.com')
req.add_header('Authorization', 'Basic %s' % auth_encoded)
try:
    response = urllib2.urlopen(req)
except urllib2.HTTPError, http_e:
    # etc...
    pass
Sign up to request clarification or add additional context in comments.

1 Comment

request.add_header('Authorization', b'Basic ' + base64.b64encode(username + b':' + password))
5

Please read about the urllib2 password manager and the basic authentication handler as well as the digest authentication handler.

http://docs.python.org/library/urllib2.html#abstractbasicauthhandler-objects

http://docs.python.org/library/urllib2.html#httpdigestauthhandler-objects

Your urllib2 script must actually provide enough information to do HTTP authentication. Usernames, Passwords, Domains, etc.

Comments

3

If you want to pass username and password information to urllib2 you'll need to use an HTTPBasicAuthHandler.

Here's a tutorial showing you how to do it.

Comments

2

You cannot pass credentials to urllib2.open like that. In your case, user is interpreted as the domain name, while [email protected] is interpreted as the port number.

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.