Connecting via TOR raises error

1.4k views Asked by At

I'm having trouble with connecting to the Internet via TOR. I've followed all instructions from this youtube video but I'm still getting error:

Connected to Tor # printed string

Traceback (most recent call last):
  ....
  ....

  File "C:\Python27\lib\httplib.py", line 787, in connect
    self.timeout, self.source_address)
  File "C:\Python27\lib\socket.py", line 571, in create_connection
    raise err
socket.error: [Errno 10061] No connection could be made because the target machine actively refused it  

I'm attaching the code:

import httplib
import socket
import socks

port = 9050
url = 'my-ip.heroku.com'

def connectTor():
    socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", port, True)
    socket.socket = socks.socksocket

def main():
    connectTor()

    print("Connected to Tor")
    conn = httplib.HTTPConnection(url)
    conn.request("GET", "/")
    response = conn.getresponse()
    print(response.read())

if __name__ == "__main__":
    main()

I'm confused because it is said that I have to download TOR from this page: https://www.torproject.org/ - when I installed it, it worked - I could start TOR browser and browse. But this guy has a Vidalia control panel which I don't have.

Where could be the problem?

1

There are 1 answers

0
Louis Thibault On

Instead of using sockets directly, you should use the requesocks library to route your HTTP requests through your locally-running SOCKS proxy.

From there, configure your Session object to use your local proxy for http(s) and then issue arbitrary requests:

session = requests.session()
session.proxies = {'http': 'socks5://127.0.0.1:9050',
                   'https': 'socks5://127.0.0.1:9050'}
resp = session.get('https://api.github.com', auth=('user', 'pass'))
print(resp.status_code)
print(resp.text)