本文介绍了如何在 Python 中更改 Tor 身份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下脚本:

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 和 SocksiPy

现在我想为每个请求更改 tor 身份,例如:

Now I want to change tor identity with each request, for example:

for i in range(0, 10):
   #somehow change tor identity
   print(urllib2.urlopen("http://www.ifconfig.me/ip").read())

我该怎么做?

推荐答案

今天搜索了很多关于这个问题,终于自己回答了.但在我需要说 pirvoxy 和 tor 应该正确配置之前.第一个脚本,然后是关于配置的一点:

Today, I have searched a lot about this question, and finally managed to answer myself. But before I need to say that pirvoxy and tor should be configured correctly. First script, then a little bit about configuration:

import urllib2
from TorCtl import TorCtl

proxy_support = urllib2.ProxyHandler({"http" : "127.0.0.1:8118"})
opener = urllib2.build_opener(proxy_support) 

def newId():
    conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051, passphrase="your_password")
    conn.send_signal("NEWNYM")

for i in range(0, 10):
    print "case "+str(i+1)
    newId()
    proxy_support = urllib2.ProxyHandler({"http" : "127.0.0.1:8118"})
    urllib2.install_opener(opener)
    print(urllib2.urlopen("http://www.ifconfig.me/ip").read())

以上脚本获取新 IP 并从 ifconfig.me 网站进行检查.关于配置:我们需要 Privoxy.要将 TOR 与 HTTP 连接一起使用,privoxy 应该与 Tor 一起使用.我们可以通过将 thi 添加到/etc/privoxy/config 文件来实现:

Above script gets new IP and checks it from ifconfig.me web site. About configuration:We need Privoxy. to use TOR with HTTP connections, privoxy should work with tor. We can do it by adding thi to /etc/privoxy/config file:

forward-socks5 / localhost:9050 . #dot is important at the end

然后我们在/etc/tor/torrc 文件中配置 ControlPort.我们只需要取消注释这一行:

then we configure ControlPort in /etc/tor/torrc file. We need just uncomment this line:

ControlPort 9051
## If you enable the controlport, be sure to enable one of these
## authentication methods, to prevent attackers from accessing it.
HashedControlPassword 16:872860B76453A77D60CA2BB8C1A7042072093276A3D701AD684053EC4C

然后我们只需重新启动tor:

then we just restart tor:

/etc/init.d/tor restart

这篇关于如何在 Python 中更改 Tor 身份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 19:33