Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Handling cookies without urllib2 and cookielib

Reply
Thread Tools

Handling cookies without urllib2 and cookielib

 
 
Joshua Kugler
Guest
Posts: n/a
 
      12-15-2007
Standard disclaimer: read, googled, read some more. If you have a link,
please free free to point me there.

I'm using HTTPlib to construct some functional tests for a web app we're
writing. We're not using urllib2 because we need support for PUT and
DELETE methods, which urllib2 does not do.

We also need client-side cookie handling. So, I start reading about
cookielib and run across a problem. It's cookie handling is tied quite
tightly to urllib2's request object. httplib has somewhat different
semantics in its request object. So, you can use cookielib with httplib.
And cookie lib has no simple function (that I could find) for passing in a
set-cookie header and getting back a CookieJar object (or even a list of
Cookie objects).

I'm sure I'm not the first to have to deal with httplib and cookies. Anyone
have suggestions or pointers?

j

 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      12-16-2007
On 14 dic, 23:44, Joshua Kugler <(E-Mail Removed)> wrote:

> I'm using HTTPlib to construct some functional tests for a web app we're
> writing. We're not using urllib2 because we need support for PUT and
> DELETE methods, which urllib2 does not do.
>
> We also need client-side cookie handling. So, I start reading about
> cookielib and run across a problem. It's cookie handling is tied quite
> tightly to urllib2's request object. httplib has somewhat different
> semantics in its request object. So, you can use cookielib with httplib.
> And cookie lib has no simple function (that I could find) for passing in a
> set-cookie header and getting back a CookieJar object (or even a list of
> Cookie objects).


What about correcting the first thing, making urllib2 support HEAD/PUT/
DELETE?

import urllib2

class Request(urllib2.Request):

def __init__(self, url, data=None, headers={},
origin_req_host=None, unverifiable=False,
method=None):
urllib2.Request.__init__(self, url, data, headers,
origin_req_host, unverifiable)
self.method = method

def get_method(self):
if self.method is None:
if self.data is not None:
return "POST"
else:
return "GET"
return self.method

py> f = urllib2.urlopen(Request("http://www.python.org/",
method="HEAD"))
py> print f.info()
Date: Sun, 16 Dec 2007 00:03:43 GMT
Server: Apache/2.2.3 (Debian) DAV/2 SVN/1.4.2 mod_ssl/2.2.3 OpenSSL/
0.9.8c
Last-Modified: Sat, 15 Dec 2007 16:25:58 GMT
ETag: "60193-3e6a-a24fb180"
Accept-Ranges: bytes
Content-Length: 15978
Connection: close
Content-Type: text/html

py> print len(f.read())
0

Notes:
a) Instead of urlopen(url,...) you must use urlopen(Request(url,...))
b) Redirection is not handled correctly in HTTPRedirectHandler (the
request method should be copied over)
c) I've not verified PUT / DELETE methods
d) I'll try to make a proper patch later

--
Gabriel Genellina
 
Reply With Quote
 
 
 
 
Joshua Kugler
Guest
Posts: n/a
 
      12-18-2007
Gabriel Genellina wrote:

> On 14 dic, 23:44, Joshua Kugler <(E-Mail Removed)> wrote:
>
>> I'm using HTTPlib to construct some functional tests for a web app we're
>> writing. We're not using urllib2 because we need support for PUT and
>> DELETE methods, which urllib2 does not do.
>>
>> We also need client-side cookie handling. So, I start reading about
>> cookielib and run across a problem. It's cookie handling is tied quite
>> tightly to urllib2's request object. httplib has somewhat different
>> semantics in its request object. So, you can use cookielib with httplib.
>> And cookie lib has no simple function (that I could find) for passing in
>> a set-cookie header and getting back a CookieJar object (or even a list
>> of Cookie objects).

>
> What about correcting the first thing, making urllib2 support HEAD/PUT/
> DELETE?

<SNIP>

We may have to do that, and then hack on the Redirect handler too so it will
properly keep the request method. But that's not our preference, for
obvious reasons.

I just find it hard to believe that no one has ever needed to do cookie
handling in a generic way (i.e. input: set-cookie header, output: cookie
objects) before. May have to write my own. Or sublcass/extend cookielib.

j

 
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
[cookielib] How to add cookies myself? Gilles Ganault Python 0 12-16-2008 02:41 PM
Re: RFC 2965 cookies, cookielib, and mailman. Titus Brown Python 0 02-13-2005 08:49 PM
RFC 2965 cookies, cookielib, and mailman. John J Lee Python 0 01-30-2005 10:19 PM
cookielib and urllib2: thread-safe? Alex Hunsley Python 1 01-29-2005 12:07 PM
RFC 2965 cookies, cookielib, and mailman. C. Titus Brown Python 0 12-27-2004 08:41 PM



Advertisments