Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用TOR运行python脚本_Python_Linux_Proxy_Tor - Fatal编程技术网

使用TOR运行python脚本

使用TOR运行python脚本,python,linux,proxy,tor,Python,Linux,Proxy,Tor,大家好!首先,我想确保有类似的话题,但没有公认的答案或明确的回应。所以我想把它们结合起来再问一次。我有以下脚本: import urllib2 proxy = urllib2.ProxyHandler({"http":"127.0.0.1:9050"}) opener = urllib2.build_opener(proxy) print(opener.open("http://www.ifconfig.me/ip").read()) 我想匿名运行它,例如使用with tor。但它给出了这样

大家好!首先,我想确保有类似的话题,但没有公认的答案或明确的回应。所以我想把它们结合起来再问一次。我有以下脚本:

import urllib2

proxy = urllib2.ProxyHandler({"http":"127.0.0.1:9050"})
opener = urllib2.build_opener(proxy)
print(opener.open("http://www.ifconfig.me/ip").read())
我想匿名运行它,例如使用with tor。但它给出了这样一个错误:

Traceback (most recent call last):
  File "python_tor.py", line 5, in <module>
    print(opener.open("http://www.ifconfig.me/ip").read())
  File "/usr/lib/python2.7/urllib2.py", line 400, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 513, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 438, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 372, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 521, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 501: Tor is not an HTTP Proxy
以及对这个话题的评论:

It may be worthwhile for people reading this thread to know that port 8118 is actually Privoxy's port, not Tor. Tor is a strictly SOCKS-only proxy (port 9050) so it rejects all non-SOCKS traffic (e.g. HTTP). To handle non-SOCKS traffic, you would need to use Privoxy (port 8118) or Polipo (port 8123) to translate the traffic into SOCKS so Tor would accept.

Privoxy is better for privacy and Polipo is better for performance because it does caching.

有人能解释一下如何匿名执行我的脚本吗?

你看过错误消息了吗?它说“501 Tor不是HTTP代理”


它是SOCKS代理,而不是HTTP代理。试着像这样使用它:

使用给定的答案,这是可能的:

import socks
import socket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)
socket.socket = socks.socksocket
import urllib2

print(urllib2.urlopen("http://www.ifconfig.me/ip").read())

但我感到惊讶的是,每一个新的请求都有可能改变tor的ip地址吗?

可能的重复应该会引发一个新的问题。这需要tor控制协议。将“信号新名称”发送至127.0.0.1:9051。xt
import socks
import socket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)
socket.socket = socks.socksocket
import urllib2

print(urllib2.urlopen("http://www.ifconfig.me/ip").read())