如何使用python通过tor成功访问网站?端口 8118 和 9050 不起作用?
我正在尝试通过 tor 使用 python 访问网站,但遇到问题。我开始尝试这个线程和其中引用的线程: How to make urllib2 requests through Tor in Python?
首先,我尝试了原始代码片段:
import urllib2
proxy_handler = urllib2.ProxyHandler({"tcp":"http://127.0.0.1:9050"})
opener = urllib2.build_opener(proxy_handler)
urllib2.install_opener(opener)
然后我尝试了其中一个答案中发布的修改后的代码,人们说这对他们有用。不幸的是,代码可以工作,因为它可以下载页面,但它无法工作,因为我的 IP 地址仍然相同:
proxy_support = urllib2.ProxyHandler({"http" : "127.0.0.1:8118"})
opener = urllib2.build_opener(proxy_support)
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
print opener.open('http://www.google.com').read()
我按照 Ubuntu 和 TOR 站点在标准配置中设置了 TOR相应的文档,并且 nmap 显示在端口 9050 上运行的 TOR tcp 代理: 9050/tcp open tor-socks
但是,当我运行上述任一脚本时,我的 IP 地址没有更改。 python 是否不尊重 http 环境变量,或者是否存在我遗漏的代码问题?
I'm trying to access a website with python through tor, but I'm having problems. I started my attempts with this thread and the one referenced in it: How to make urllib2 requests through Tor in Python?
First I tried the original code snippet:
import urllib2
proxy_handler = urllib2.ProxyHandler({"tcp":"http://127.0.0.1:9050"})
opener = urllib2.build_opener(proxy_handler)
urllib2.install_opener(opener)
then I tried the modified code posted in one of the answers, which people said worked for them. Unfortunately, the code works in that it downloads the page, but it doesn't work because my IP address is still the same:
proxy_support = urllib2.ProxyHandler({"http" : "127.0.0.1:8118"})
opener = urllib2.build_opener(proxy_support)
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
print opener.open('http://www.google.com').read()
I have TOR set up in the standard configuration, per the Ubuntu and TOR sites respective documentation, and nmap shows the TOR tcp proxy running on port 9050: 9050/tcp open tor-socks
However, my IP address isn't changed when I run either of the above scripts. Is python not respecting the http environment variables, or is there a code problem that I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TOR 提供 SOCKS 代理。由于 urllib2 只能处理 HTTP 代理,因此您必须 使用 SOCKS 实现。
TOR provides a SOCKS proxy. Since urllib2 can only handle HTTP proxies, you'll have to use a SOCKS implementation.