Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > HTTP Authentication

Reply
Thread Tools

HTTP Authentication

 
 
Lakshman
Guest
Posts: n/a
 
      04-06-2009
Whats is the python urllib2 equivallent of

curl -u usernameassword 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?
 
Reply With Quote
 
 
 
 
Gerhard Häring
Guest
Posts: n/a
 
      04-06-2009
Lakshman wrote:
> Whats is the python urllib2 equivallent of
>
> curl -u usernameassword 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?


Better? Yes.
Simpler? No.

Actually, the proper way using the urllib2 API is more code.

When I need it some time ago, I googled and used this recipe:

http://www.voidspace.org.uk/python/a...lib2.shtml#id6


# create a password manager
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()

# Add the username and password.
# If we knew the realm, we could use it instead of ``None``.
top_level_url = "http://example.com/foo/"
password_mgr.add_password(None, top_level_url, username, password)

handler = urllib2.HTTPBasicAuthHandler(password_mgr)

# create "opener" (OpenerDirector instance)
opener = urllib2.build_opener(handler)

# use the opener to fetch a URL
opener.open(a_url)

# Install the opener.
# Now all calls to urllib2.urlopen use our opener.
urllib2.install_opener(opener)

-- Gerhard

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Send Basic HTTP authentication credential in the first HTTP request Nacho Nachev ASP .Net Web Services 2 09-05-2012 08:49 PM
HTTP SOAP/HTTP GET/HTTP POST milan_9211 Software 0 01-10-2011 02:10 PM
HTTP basic authentication with form-based authentication Max Python 2 08-08-2008 06:16 PM
Split Tunnel Blocks http through tunnel but passes http around tunnel a.nonny mouse Cisco 2 09-19-2004 12:10 AM
Getting "HTTP Error 403 - Forbidden" at http://localhost/quickstart/ASPPlus/ Scott MCSD 1 08-04-2004 05:28 PM



Advertisments