我是urllib2的新手。我尝试使用urllib2将ip地址绑定到URL请求,它不适用于tor。
给出的ip地址import socks
import socket
import urllib2
true_socket = socket.socket
def make_bound_socket(source_ip):
def bound_socket(*a, **k):
sock = true_socket(*a, **k)
sock.bind((source_ip, 0))
return sock
return bound_socket
if __name__ == '__main__':
socket.socket = make_bound_socket('123.108.224.70')
print "IP address: from tor-",urllib2.urlopen('http://my-ip.herokuapp.com').read()
我提到的IP地址是 tor 。谢谢先进
它给了我以下错误
Traceback (most recent call last):
File "socks_ppp.py", line 56, in <module>
newIdentity()
File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 404, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 422, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1214, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1184, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 99] Cannot assign requested address>
答案 0 :(得分:1)
您要求绑定到与IP地址123.108.224.70
对应的接口。如果您的计算机上没有相应的接口,则无法绑定套接字,您将看到错误。这是一个简化的例子:
>>> import socket
>>> s = socket.socket()
>>> s.bind(('123.108.224.70', 0))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 99] Cannot assign requested address
这失败了,因为我没有与该IP地址的接口,我怀疑你也一样 - 尝试一下。
您可以使用ifconfig
查看哪些IP地址对应于某个界面。我怀疑123.108.224.70
不存在。
否则,当我使用我的机器上存在的IP地址时,我发现您的代码工作正常。