urllib2 / ClientCookie / Keep-Alive

Richie Hindle richie at entrian.com
Fri Oct 15 16:18:25 CEST 2004


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.)

-- 
Richie Hindle
richie at entrian.com




More information about the Python-list mailing list