15

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?

bignose
  • 30,281
  • 14
  • 77
  • 110
lprsd
  • 84,407
  • 47
  • 135
  • 168

2 Answers2

20

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
  • 3,481
  • 1
  • 25
  • 29
  • 2
    How is this simpler than the initial example? – ibz Jun 28 '09 at 16:57
  • 1
    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 Jul 16 '09 at 13:12
  • I found the passman stuff didn't work. Adding the base64 user/pass header as per this answer http://stackoverflow.com/a/18592800/623159 did work. Accessing jenkins URL like http:///job//lastCompletedBuild/testR‌​eport/api/python – gaoithe Jan 19 '17 at 15:21
6

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