python 指定出口IP 多线程
import functools
import httplib
import urllib2
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)
def test(i):
handler = BoundHTTPHandler(source_address=("192.168.1."+str(i), 0))
opener = urllib2.build_opener(handler)
#urllib2.install_opener(opener)
print i,opener.open("http://ipv4.lookup.test-ipv6.com/").read()
for i in xrange(100):
Thread(target=test, args=(i,)).start()
quit()