Reading https url from Python using basic access authentication

How to open https url in Python?

import urllib2 url = "https://user: password@domain.com /path/ f = urllib2.urlopen(url) print f.read() 

gives:

 httplib.InvalidURL: nonnumeric port: ' password@domain.com ' 
+4
source share
4 answers

Read about the urllib2 password manager and the basic authentication handler, as well as the digest verification handler.

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

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

Your urllib2 script should actually provide sufficient information for HTTP authentication. Usernames, passwords, domains, etc.

+5
source

It never let me down.

 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 
+11
source

If you want to pass username and password information to urllib2 , you need to use HTTPBasicAuthHandler .

Here is a tutorial showing you how to do this.

+3
source

You cannot pass urllib2.open this way. In your case, user interpreted as a domain name, and password@domain.com interpreted as a port number.

+2
source

All Articles