我想使用urllib2并从不同的IP地址发出请求。在
我已经检查过了,但我没有运气:Source interface with Python and 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)
# test
handler = BoundHTTPHandler("192.168.1.1", 0)
opener = urllib2.build_opener(handler)
urllib2.install_opener(opener)
urllib2.urlopen("http://google.com/").read()
错误:TypeError:init()获取了意外的关键字参数“source_address”
在使用urllib2之前如何运行这些代码?在
^{pr2}$有了bound_socket函数,然后呢?在
Edit我不相信我的python版本支持源地址,这就是我得到错误的原因,我想。在
那么,让我们试试套接字代码。在
代码
socket.socket = bound_socket
会影响任何模块中在其之后运行的所有代码,也就是说,一旦您运行了它,您就不需要做任何其他事情。在^{} class has ^{} parameter in Python 2.7 因此你的
BoundHTTPHandler
也应该可以工作。在相关问题 更多 >
编程相关推荐