HTTP Authentication in Python

2020-02-08 06:21发布

问题:

Whats is the python urllib equivallent of

curl -u username:password status="abcd" http://example.com/update.json

I did this:

handle = urllib2.Request(url)
authheader =  "Basic %s" % base64.encodestring('%s:%s' % (username, password))
handle.add_header("Authorization", authheader)

Is there a better / simpler way?

回答1:

The trick is to create a password manager, and then tell urllib about it. Usually, you won't care about the realm of the authentication, just the host/url part. For example, the following:

password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
top_level_url = "http://example.com/"
password_mgr.add_password(None, top_level_url, 'user', 'password')
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(urllib2.HTTPHandler, handler)
request = urllib2.Request(url)

Will set the user name and password to every URL starting with top_level_url. Other options are to specify a host name or more complete URL here.

A good document describing this and more is at http://www.voidspace.org.uk/python/articles/urllib2.shtml#id6.



回答2:

Yes, have a look at the urllib2.HTTP*AuthHandlers.

Example from the documentation:

import urllib2
# Create an OpenerDirector with support for Basic HTTP Authentication...
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(realm='PDQ Application',
                          uri='https://mahler:8092/site-updates.py',
                          user='klem',
                          passwd='kadidd!ehopper')
opener = urllib2.build_opener(auth_handler)
# ...and install it globally so it can be used with urlopen.
urllib2.install_opener(opener)
urllib2.urlopen('http://www.example.com/login.html')