如何在Python中更改Tor身份?

时间:2012-03-27 10:13:08

标签: 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身份,例如:

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

我该怎么做?

7 个答案:

答案 0 :(得分:28)

Tor用Python编写了一个新的TOR控件库,stem。它可以在PyPI找到。他们提供了一些很好的教程,如何使用它,one解释了如何改变你的身份:

from stem import Signal
from stem.control import Controller

with Controller.from_port(port = 9051) as controller:
  controller.authenticate()
  controller.signal(Signal.NEWNYM)

确保您的配置为correct

答案 1 :(得分:20)

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

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文件来实现:

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

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

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:

/etc/init.d/tor restart

答案 2 :(得分:13)

另一个简单的解决方案,无需外部库,适用于IPv4和IPv6:

import socket

try:
    tor_c = socket.create_connection((TOR_CTRL_HOST, TOR_CTRL_PORT))
    tor_c.send('AUTHENTICATE "{}"\r\nSIGNAL NEWNYM\r\n'.format(TOR_CTRL_PWD))
    response = tor_c.recv(1024)
    if response != '250 OK\r\n250 OK\r\n':
        sys.stderr.write('Unexpected response from Tor control port: {}\n'.format(response))
except Exception, e:
    sys.stderr.write('Error connecting to Tor control port: {}\n'.format(repr(e)))

答案 3 :(得分:3)

This is a video我使用STEM,SockSipy,Tor 100%工作:)

#!/usr/bin/python
import socks
import socket
import time
from stem.control import Controller
from stem import Signal
import urllib2
import sys

def info():
    print "[*] Welcome to Chart-Cheat Script"
    print "[*] This script works with running TOR only"
    print "[*] usage is chartcheat.py domain"
    print "[*] argument domain must be in format www.example.com"
    print "[*] Example: chartcheat.py www.example.com"
    return

if len(sys.argv)==2:
    info();
    counter = 0
    url = str(sys.argv[1]);
    with Controller.from_port(port = 9051) as controller:
        controller.authenticate()
        socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)
        socket.socket = socks.socksocket
        #visiting url in infinite loop      
        while True:
            urllib2.urlopen("http://"+url)
            counter=counter+1
            print "Page " + url + " visited = " + str(counter)
            #wait till next identity will be available
            controller.signal(Signal.NEWNYM)
            time.sleep(controller.get_newnym_wait())            
else:
    info();

如果你正在运行python3,python3中的urllib包将与python2中的urllib2包相同。

答案 4 :(得分:0)

你可以这样写:

def renew_connection():
    with Controller.from_port(port=9051) as controller:
        controller.authenticate(password='password')
        controller.signal(Signal.NEWNYM)
        controller.close()

def request_tor(url, headers):
    renew_connection()
    session = requests.session()
    session.proxies = {}
    session.proxies['http'] = 'socks5h://localhost:9050'
    print((session.get(url)).text)

答案 5 :(得分:-1)

您可以通过取消注释

中的几行来启用tor控制服务器
/etc/tor/torrc

使用词库发送NEWNYM信号来改变电路。

controller.signal(Signal.NEWNYM)

You can read tutorial here.

答案 6 :(得分:-2)

以下可行:

for i in range(0, 10):
   #somehow change tor identity
   socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050+i)
   socket.socket = socks.socksocket

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

您基本上在进行每次连接之前设置代理。我假设您有不同的IP代理,因为您尚未说明您打算如何更改IP