4

Many people have shown how to use Tor in Python like this:

proxy_support = urllib2.ProxyHandler({"http" : "127.0.0.1:8118"})
opener = urllib2.build_opener(proxy_support) 
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
print opener.open(url).read()

However, I'm hunting for a python bindings to Tor, that to be integrated into my App.

I mean, users will not need to download and install and configure Tor again.

That would be very useful, help please.

Thanks.

3 Answers3

2

There are a few python libraries, the most common ones being stem and txtorcon. As for directing traffic over tor, you might find stem's client usage tutorial to be helpful.

Damian
  • 2,944
  • 2
  • 18
  • 15
0

You can use the tor control port to setup tor, the reason there aren't any bindings to tor is because tor effectively already has an API. You would still need to use tor as a SOCKS proxy, which most python libraries don't support very well.

BenH
  • 894
  • 8
  • 7
0

If depending on Twisted is fine, you can try the txtorcon library which speaks to Tor via the control port. It supports the endpoints API for hidden services, has documentation, examples and 95%+ unit-test coverage. There are abstractions for continuously-updated state (i.e. the current state of Tor, with streams, circuits, etcetera) and configuration (setting and reading).

It does depend upon a locally installed tor -- that is, "tor" must be in the path, OR must already be running and listening for control connections. If you're on a debian-based system "apt-get install tor" is sufficient for both of these to be satisfied.

mike
  • 403
  • 3
  • 6