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?
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:
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.