如何使用Socksipy迭代代理列表

时间:2011-08-16 13:48:47

标签: python sockets proxy

出于某种原因,我可以让这个工作,使用单一代理一切似乎都很好。

#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()

Error:

#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()

1 个答案:

答案 0 :(得分: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地址。摆脱它,你应该没事。