如何用Socksipy遍历代理列表
出于某种原因,我可以让这个工作,使用单个代理的时候一切似乎都正常。
#This works
import socks
import urllib2
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '68.xx.193.xx', 666)
socks.wrapmodule(urllib2)
print urllib2.urlopen('http://automation.whatismyip.com/n09230945.asp').read()
&
#This doesn't import socks import urllib2 proxies=['68.xx.193.xx','xx.178.xx.70','98.xx.84.xx','83.xx.86.xx'] ports=[666,1080,859,910] for i in range(len(proxies)): socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, repr(proxies[i]), ports[i]) socks.wrapmodule(urllib2) print urllib2.urlopen('http://automation.whatismyip.com/n09230945.asp').read()
错误:
Traceback (most recent call last): File "/home/zer0/Aptana Studio 3 Workspace/sy/src/test.py", line 38, in <module> print urllib2.urlopen('http://automation.whatismyip.com/n09230945.asp').read() File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen return _opener.open(url, data, timeout) File "/usr/lib/python2.7/urllib2.py", line 391, in open response = self._open(req, data) File "/usr/lib/python2.7/urllib2.py", line 409, in _open '_open', req) File "/usr/lib/python2.7/urllib2.py", line 369, in _call_chain result = func(*args) File "/usr/lib/python2.7/urllib2.py", line 1185, in http_open return self.do_open(httplib.HTTPConnection, req) File "/usr/lib/python2.7/urllib2.py", line 1160, in do_open raise URLError(err) urllib2.URLError: <urlopen error [Errno -5] No address associated with hostname>
1 个回答
0
运行这个:
proxies=['68.xx.193.xx','xx.178.xx.70','98.xx.84.xx','83.xx.86.xx']
ports=[666,1080,859,910]
for i in range(len(proxies)):
print (repr(proxies[i]), ports[i])
你会得到:
("'68.xx.193.xx'", 666)
("'xx.178.xx.70'", 1080)
("'98.xx.84.xx'", 859)
("'83.xx.86.xx'", 910)
你在用 repr
这个函数的时候,添加了不需要的引号,所以 urllib2
把它当成了主机名,而不是IP地址。把它去掉就没问题了。