Cookies in request object

Garth Grimm garth_grimm at hp.com
Tue Aug 14 20:33:03 EDT 2001


I feel that I should have been able to figure this out on my own, or by
using the python docs, but I haven't been able to.

How do I attach a Cookie object to an HTTP request object?

Or more precisely...

I have a script that requests and processes a web page...

# Construct HTTP request
h = httplib.HTTP('my.domain.com')
h.putrequest('GET', '/examples/servlet/RequestHeaderExample')
h.endheaders()

# Send request and retrieve response
errcode, errmsg, headers = h.getreply()

# Display the response code and headers
write("<br>Code: " + str(errcode) + "<br>Msg: " + errmsg )
for name in headers.keys():
    write("<br>" + name + " = " + headers.getheader(name) )

# Display the response page
f = h.getfile()
write( f.read() )

This code works fine.  BTW, the target URL is a servlet that returns a page
who's body includes the original request's header information.

The Cookie code I'm trying to test is:

C = Cookie.SimpleCookie()
C.load("chips=ahoy; vienna=finger")

and the Cookie object seems to be constructed correctly, since if I do a
write( C.output() ) I get what I'd expect.

Unfortunately, when I insert the Cookie code just before the h.endheaders()
call above (which is where I think it would belong), the target server
claims I'm not sending a cookie.  I can understand it's complaint, since I
really don't see a command to connect the Cookie object to the HTTP object.

Any help (or requests for more information that may lead to help) would be
greatly appreciated.

TIA,
--
Garth







More information about the Python-list mailing list