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
settings.iniPHP Code:
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 = {}
PHP Code:
[Tor]
SocksListenAddress=127.0.0.1
SocksPort=9051
ControlPort=9151
Password=test
Results 1 to 3 of 3
Thread: Python Tor Example
- 31 Mar. 2013 12:08am #1
Python Tor Example
Last edited by Tree; 01 Apr. 2013 at 12:42am.
- 31 Mar. 2013 12:12am #2
This sounds like a great idea to be honest. Bookmarked in case I ever get around to actually writing something.
- 31 Mar. 2013 12:14am #3
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.