A preserved archive of the Logical Gamers community forums, 2009-2025. The original threads and posts, served read-only. Registration, posting and private messages are gone for good.

Python Tor Example

1.1k views · started by Tree ·
#1
Python Tor Example
I do not vouch for the readiness, or standards of this release. There are many things I would remake in it, I just do not have a need for it right now.

When I was using my item gen, I made a Tor module that uses TorCTL to easily create connections, test connections, change identity, and check IP. From what I was doing with TorCTL it did not have an error check if Tor wasn't actually running, so I had to initialize it all through a socket object, which lead to this. It's pretty simple, kinda dirty but maybe someone here will have a use for it, or will be able to learn something about implementing Tor into their projects. This module was made based off me running the minimal tor package, not vindilia, but they should both work if you set the proper ports and password.

tor.py
import requests
import socks
import socket
import sys
import time
from ConfigParser import ConfigParser
from TorCtl import TorCtl

def parse_ini(ini_file='settings.ini'):
config = ConfigParser()
try:
config.read(ini_file)
tor.update(dict(config.items('Tor')))
except:
raise Exception('Settings missing or incorrect.')

def connect():
parse_ini()
while 'control' not in tor:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((tor['sockslistenaddress'], int(tor['controlport'])))
tor['control'] = TorCtl.Connection(sock)
tor['control'].authenticate(tor['password'])
except socket.error:
print 'Unable to connect to Tor.'
raw_input('Press any key to exit.')
sys.exit()
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5,
tor['sockslistenaddress'],
int(tor['socksport']))
socket.socket = socks.socksocket
socket.create_connection = create_connection

httpget = requests.get('http://jsonip.com/').json()
tor['ip'] = httpget['ip']

def create_connection(address, timeout=None, source_address=None):
sock = socks.socksocket()
sock.connect(address)
return sock

def new_id():
new_ip = tor['ip']
while new_ip == tor['ip']:
tor['control'].send_signal("NEWNYM")
httpget = requests.get('http://jsonip.com/').json()
new_ip = httpget['ip']
time.sleep(.5)
tor['ip'] = new_ip

tor = {}


settings.ini

[tor]
SocksListenAddress=127.0.0.1
SocksPort=9051
ControlPort=9151
Password=test
[/tor]
#2
This sounds like a great idea to be honest. Bookmarked in case I ever get around to actually writing something.
#3
Artificial wrote:
This sounds like a great idea to be honest. Bookmarked in case I ever get around to actually writing something.


Yeah, it was useful but I normally prefer speed over anonymity. I kinda forgot why I did some parts in it, and I'd probably redo most of it (especially how I was reading the config) but overall it works and might be a good example for others. Really the only reason I even made this was because my code looked horribly messy with it in the main section, and it was becoming quite long since TorCTL doesn't seem to have any actually exception raise if Tor isn't detected, but instead simply prints out that Tor wasn't running and then continues. On that note, I may also release my IP range and port scanner. It can do a full IP range with all ports scanned in about 40 seconds or so, but I'm currently experimenting trying to scan all ports from 20 to 10,000 and seeing how fast I can make that but I kinda ditched it and started other stuff.