Vegard J.
Vegard J.

Reputation: 307

integrating tor and python

I want to know how i can access a webpage through Tor, in Python.

There are a few similar questions, but none of them are completely answered and alot of the answers outright wrong.

The first page i stumbled upon was this; How to make urllib2 requests through Tor in Python?, and the most popular answer on there is;

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('http://www.google.com').read()

there are a few pages with similar answers, anyway somebody on that page commented;

"It may be worthwhile for people reading this thread to know that port 8118 is actually Privoxy's port, not Tor. Tor is a strictly SOCKS-only proxy (port 9050) so it rejects all non-SOCKS traffic (e.g. HTTP). To handle non-SOCKS traffic, you would need to use Privoxy (port 8118) or Polipo (port 8123) to translate the traffic into SOCKS so Tor would accept.

Privoxy is better for privacy and Polipo is better for performance because it does caching."

so this wont get me anywhere, after some searching around i found this Python urllib over TOR?

i downloaded socksipy, and tried the code. it works, but i get the same error as this guy. theres no accepted answer to his problem. the post is from 2011, i thought a new question where i try to clarify was in order.

alternatively, is there a good library for dealing with Tor?

Upvotes: 2

Views: 2044

Answers (2)

Damian
Damian

Reputation: 2954

Yup, we have a couple python libraries for working with Tor, the most common ones being stem and txtorcon. For a tutorial on that kind of client usage see here.

Upvotes: 2

Vegard J.
Vegard J.

Reputation: 307

alright, this is the solution i used;

i downloaded this - http://pastie.org/6002288 (cant remember the source, but with the information stated you can probably track it down if you need to)

i run it

now i change the settings of my computer, to force any dns resolutions to go through my proxy nameserver locally on port 53. it relays it to tor.

that takes care of it, now i can run the following code knowing that im secure!

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

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)

# patch the socket module
socket.socket = socks.socksocket
socket.create_connection = create_connection


import urllib

url = "http://www.google.com"
urllib.urlopen(url)

Tor still tells me its only receiving an IP address, not a url, and i may be leaking information. but im positive im not, because the DNS lookups are going through my local proxy which goes through Tor.

Upvotes: 1

Related Questions