Python - Controlling Tor

半世苍凉 提交于 2019-11-30 05:11:15

Well, out of luck I managed to find a PHP script that did the exact same thing I wanted, and with the help of that I converted it to work in TorCtl. This is what it looks like for anyone else needing it in the future!

from TorCtl import TorCtl

conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051, passphrase="123")

TorCtl.Connection.send_signal(conn, "NEWNYM")

You can use a similar code in python:

def renewTorIdentity(self, passAuth):
    try:
        s = socket.socket()
        s.connect(('localhost', 9051))
        s.send('AUTHENTICATE "{0}"\r\n'.format(passAuth))
        resp = s.recv(1024)

        if resp.startswith('250'):
            s.send("signal NEWNYM\r\n")
            resp = s.recv(1024)

            if resp.startswith('250'):
                print "Identity renewed"
            else:
                print "response 2:", resp

        else:
            print "response 1:", resp

    except Exception as e:
        print "Can't renew identity: ", e 

You can check this post for a mini-tutorial

Apparently the stem package works better. You can install tor on your computer and keep it running in terminal. Then run the following program:

from stem import Signal
from stem.control import Controller

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

stem is the official package developed by tor.org, and you can see their documentation

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