不同IP地址的请求

2024-09-28 22:40:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用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版本支持源地址,这就是我得到错误的原因,我想。在

那么,让我们试试套接字代码。在


Tags: 代码selfhttpsourceinitaddressdefopen