Python fails Tor check using urllib2 to initiate requests

房东的猫 提交于 2019-12-10 16:00:42

问题


After reading through the other questions on StackOverflow, I got a snippet of Python code that is able to make requests through a Tor proxy:

import urllib2
proxy  = urllib2.ProxyHandler({'http':'127.0.0.1:8118'})
opener = urllib2.build_opener(proxy)
print opener.open('https://check.torproject.org/').read()

Since Tor works fine in Firefox with TorButton, I expected it to work fine in Python. Unfortunately, included in the mess of HTML: Sorry. You are not using Tor. I am not sure why this is the case or how to get Tor working properly with urllib2.


回答1:


You've set up a proxy to your local Tor instance for the http protocol, but you're using https to talk to "check.torproject.org". Try:

print opener.open('http://check.torproject.org/').read()


来源:https://stackoverflow.com/questions/2075469/python-fails-tor-check-using-urllib2-to-initiate-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!