简体   繁体   English

在python中使用urllib2和Tor拒绝连接

[英]Connection refused using urllib2 and Tor in python

I am pretty new to python. 我对python很新。 I am trying to write a pretty simple web scraper for a project I am working on. 我正在尝试为我正在开发的项目编写一个非常简单的Web scraper。 In the process I am trying to use Tor to change my IP address so I don't get disconnected from the service I am scraping. 在这个过程中,我试图使用Tor来更改我的IP地址,这样我就不会与我正在抓取的服务断开连接。 I was trying to test the code specific to getting a new IP before adding it to my project. 在将其添加到我的项目之前,我试图测试特定于获取新IP的代码。 Here is the code I am testing. 这是我正在测试的代码。

from TorCtl import TorCtl
import urllib2

for i in range(1,51):
    proxy_support = urllib2.ProxyHandler({"http" : "127.0.0.1:8118"} )
    opener = urllib2.build_opener(proxy_support)
    opener.addheaders = [('User-agent', 'Mozilla/5.0')]
    urllib2.install_opener(opener)

    print "IP " + str(i) + ":"
    print urllib2.urlopen('http://ifconfig.me/ip').read()

    conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051, passphrase="torPass")
    conn.sendAndRecv('signal newnymrn')
    conn.close()

When I do this i get the following error: 当我这样做时,我收到以下错误:

IP 1: Traceback (most recent call last): File "scrapingTools.py", line 86, in main() File "scrapingTools.py", line 76, in main print urllib2.urlopen('http://ifconfig.me/ip').read() File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 126, in urlopen return _opener.open(url, data, timeout) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 394, in open response = self._open(req, data) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 412, in _open '_open', req) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 372, in _call_chain result = func(*args) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1199, in http_open return self.do_open(httplib.HTTPConnection, req) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1174, in d IP 1:回溯(最近一次调用最后一次):文件“scrapingTools.py”,第86行,在main()文件“scrapingTools.py”,第76行,在主要打印urllib2.urlopen('http://ifconfig.me / ip')。read()文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py”,第126行,在urlopen中返回_opener.open(url,data,超时)文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py”,第394行,打开响应= self._open(req,data)文件“/ System / Library / Frameworks / Python.framework / Versions / 2.7 / lib / python2.7 / urllib2.py“,第412行,在_open'_open',req)文件”/System/Library/Frameworks/Python.framework/Versions/2.7 /lib/python2.7/urllib2.py“,第372行,_call_chain结果= func(* args)文件”/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py “,第1199行,在http_open中返回self.do_open(httplib.HTTPConnection,req)文件”/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py“,第1174行, d o_open raise URLError(err) urllib2.URLError: o_open raise URLError(err)urllib2.URLError:

Any help understanding what is going on here would be greatly appreciated. 任何帮助了解这里发生的事情将不胜感激。

There is some problem with your proxy configuration. 您的代理配置存在一些问题。 Your code works without the proxy settings. 您的代码无需代理设置即可运行。

I don't know anything about TorCtl but you're not sending an AUTHENTICATE string, tor will expect that. 我对TorCtl一无所知,但是你没有发送一个AUTHENTICATE字符串,你会期待的。 It should look something like: 它应该看起来像:

telnet localhost:9051
>> 250 OK
AUTHENTICATE "xxx"
>> 250 OK
signal NEWNYM
>> 250 OK

Note, wait a few seconds for the identity to have changed. 注意,等待几秒钟以使身份发生变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM