2

I want to use urllib2 and make a request from different IP addresses.

I have checked this but I am having no luck: Source interface with Python and urllib2

Code from link:

class BoundHTTPHandler(urllib2.HTTPHandler):

    def __init__(self, source_address=None, debuglevel=0):
        urllib2.HTTPHandler.__init__(self, debuglevel)
        self.http_class = functools.partial(httplib.HTTPConnection,
                source_address=source_address)

    def http_open(self, req):
        return self.do_open(self.http_class, req)

# test
handler = BoundHTTPHandler("192.168.1.1", 0)
opener = urllib2.build_opener(handler)
urllib2.install_opener(opener)
urllib2.urlopen("http://google.com/").read() 

Error: TypeError: init() got an unexpected keyword argument 'source_address'

And how would I run this code before using urllib2?

import socket
true_socket = socket.socket
def bound_socket(*a, **k):
    sock = true_socket(*a, **k)
    sock.bind((sourceIP, 0))
    return sock
socket.socket = bound_socket

So you have the bound_socket function, then what?

Edit I don't believe my python version supports the source_address, which is why I'm getting the error, I think.

So, let's try the socket code.

Community
  • 1
  • 1
AAA
  • 1,962
  • 4
  • 30
  • 55
  • what is your python version? – jfs Oct 22 '14 at 10:23
  • here's a [code example that monkey-patches `socket`, and defines custom `HTTPSHandler()` to support SSL client/server certificates verification for `urllib2` on Python 2.4+](https://gist.github.com/zed/1347055). It shows that you can enable `source_address` for Python 2.4+ (it is simpler than enabling ssl). – jfs Oct 22 '14 at 10:39
  • Sebastian is right. What is your python version? In Python 2.6, HTTPConnection doesn't take source_address as argument for __init__() – Cristian Ciocău May 04 '15 at 13:51

1 Answers1

1

The code socket.socket = bound_socket affects all code that is run after it in any module globally i.e., once you run it; you don't need to do anything else.

httplib.HTTPConnection class has source_address parameter in Python 2.7 therefore your BoundHTTPHandler should also work.

jfs
  • 399,953
  • 195
  • 994
  • 1,670