开发者

How can I iterate through a list of proxies with Socksipy

开发者 https://www.devze.com 2023-03-28 05:41 出处:网络
For some reason I can get this to work, using single proxy everything seems fine. #This works import socks

For some reason I can get this to work, using single proxy everything seems fine.

#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:

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.开发者_如何学Gopy", 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>


Run this:

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])

You'll get

("'68.xx.193.xx'", 666)
("'xx.178.xx.70'", 1080)
("'98.xx.84.xx'", 859)
("'83.xx.86.xx'", 910)

You're adding quotes you don't want with the repr call, so urllib2 thinks it's a hostname instead of an IP address. Get rid of it and you should be fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号