Controlling source IP address within urllib2

John J. Lee jjl at
Sun Jun 5 00:30:48 CEST 2005

"Dan" <tojuso at> writes:

> Does anybody know how to control the source IP address (IPv4) when
> using the urllib2 library?  I have a Linux box with several IP
> addresses in the same subnet, and I want to simulate several
> individuals within that subnet accessing web pages independently.  I
> need the functionality of urllib2 because there will be redirects and
> other HTTP-type functions to implement.  It would be nice if I could
> create (and bind) sockets myself and then tell the urllib functions to
> use those sockets.  Perhaps there is some sort of "back-door" way of
> doing this??? Any hints are appreciated!

There's no built-in support, but I expect it's very easy to do.  I
suppose the easiest way would be to derive from
httplib.HTTPConnection, overriding .connect() to call .bind() on the
socket.  Call it BindingHTTPConnection, then define:

class BindingHTTPHandler(urllib2.HTTPHandler):
    def http_open(self, req):
        return self.do_open(BindingHTTPConnection, req)

Same goes for https:

if hasattr(httplib, 'HTTPS'):
    class BindingHTTPSHandler(urllib2.HTTPSHandler):
        def https_open(self, req):
            return self.do_open(BindingHTTPSConnection, req)

Personally, I probably wouldn't use build_opener(), but since the
above classes derive from the corresponding urllib2 classes, you can

opener = urllib2.build_opener(BindingHTTPHandler, BindingHTTPSHandler)
response ='')


More information about the Python-list mailing list