urllib2 / ClientCookie / Keep-Alive

R

Richie Hindle

Hi,

I'm trying to write a test script to hammer an HTTP server over a
persistent HTTP 1.1 connection. The server uses cookies, so I'm using a
combination of ClientCookie 0.4.19 and urllib2 with Python 2.3.4 on
Windows XP. This is what I have so far:

import urllib2, ClientCookie
UA = 'Mozilla/4.0 (compatible; MSIE 6.0;)'
URL = 'http://server/resource/'
opener = ClientCookie.build_opener(ClientCookie.HTTPCookieProcessor)
while 1:
request = ClientCookie.Request(URL)
request.add_header('User-Agent', UA)
print repr(opener.open(request).read()[:100])

Which works fine, but opens a new connection for every request.
Following some (dubious) code I found with Google, I added this:

request.add_header('Connection', 'Keep-alive')

but that makes it hang.

Can someone tell me the official way to enable urllib2 and/ClientCookie
to use persistent connections?

(I briefly tried Python 2.4, but that didn't work either. If there's
solution that requires Python 2.4, that's no problem.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top