views:

471

answers:

2

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?

+8  A: 

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.

Ivo
How is this simpler than the initial example?
ionut bizau
ibz: I never claimed it to be simpler, but in my code sample you don't have to manually manipulate the username and password strings. Plus you get all sorts of benefits from it, such as the code figuring out whether to actually send the authentication info to the server or not, based on the URL. You may or may not need all of this; pick a solution that works for you.
Ivo
+4  A: 

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')
unbeknown