[Tutor] password protection in httplib

Kent Johnson kent37 at tds.net
Fri Mar 3 14:43:05 CET 2006


Andre Engels wrote:
> 2006/3/2, Kent Johnson <kent37 at tds.net>:
> 
>>Andre Engels wrote:
>>
>>>Thanks for your help; it brought me quite a bit farther, but not as
>>>far as I wanted to come. The authentication is basic authentication,
>>>and I have been able to adapt the programs so that I now get my pages
>>>correctly.
>>>
>>>However, the program uses not only 'GET' operations, but also 'PUT'
>>>operations. These are done using httplib rather than urllib, and I
>>>cannot see at this point how I can mimick those using urllib2.
>>
>>The docs don't spell it out, but if you pass the 'data' parameter to
>>urllib2.urlopen(), it will make a POST request instead of a GET. The
>>data has to be formatted as application/x-www-form-urlencoded;
>>urllib.urlencode() will do this for you.
>>
>>So for example:
>>
>>import urllib, urllib2
>>data = dict(param1='foo', param2='bar')
>>data = urllib.urlencode(data)
>>
>># set up your basic auth as before
>>result = urllib2.urlopen('http://some.server.com', data).read()
> 
> 
> Thanks, I've gotten some further again, but the following problem is
> that I am using this connection to get some cookies, which are read
> from the page headers. As far as I can see, urllib2 puts the page
> headers into a dictionary, which is not what I need, because there are
> 4 different set-cookie headers sent out by the site, and I get only
> one "set-cookie" value. Am I right in this, and is there a way around
> it?

Have you tried using a CookieManager as shown in the first example here:
http://docs.python.org/lib/cookielib-examples.html

Once you set up your opener with a CookieJar the cookies should be 
handled automatically - if a server sets a cookie it will be remembered 
and returned back to the server on subsequent requests.

This page has more examples though again IMO they are overly complex:
http://www.voidspace.org.uk/python/articles/cookielib.shtml

Kent



More information about the Tutor mailing list