1

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?

Louis Thibault
  • 16,122
  • 21
  • 72
  • 136
Milano
  • 13,773
  • 29
  • 96
  • 240
  • 1
    I suggest you use the [stem library](https://stem.torproject.org/) instead of reinventing the wheel. – Louis Thibault Jun 28 '15 at 14:40
  • @blz I wanted to use stem library but I didn't find any tutorial 'from scratch' to know how to use it. – Milano Jun 28 '15 at 14:41
  • What do you mean by "from scratch"? There's a tutorial page with many simple examples. Are you having trouble with a specific operation involving stem? What's your end game, anyhow? – Louis Thibault Jun 28 '15 at 14:43
  • @blz The main thing is simple. To use stem (I've already installed it using pip) to get a html of some page which shows me my ip address and this ip address will be different from my default ip – Milano Jun 28 '15 at 14:45
  • Okay -- the good news is that it's fairly simple. Please see here: http://stackoverflow.com/a/15661226/1156707 – Louis Thibault Jun 28 '15 at 14:47
  • possible duplicate of [How to make python Requests work via socks proxy](http://stackoverflow.com/questions/12601316/how-to-make-python-requests-work-via-socks-proxy) – Louis Thibault Jun 28 '15 at 14:47
  • @blz Thanks but I want to use stem library or the simpliest way to use tor because I want to change my ip address often. – Milano Jun 28 '15 at 14:49
  • @blz I've decided to use tor because I don't have to search web for new proxy each time I decide to change ip. – Milano Jun 28 '15 at 14:50
  • these are two separate issues. Stem allows you to control your local Tor service (changing IP if necessary -- but **doing so is considered abuse**, and frankly, you smell like a spammer). Actually sending traffic through Tor is done with an HTTP library that's SOCKS-aware. – Louis Thibault Jun 28 '15 at 14:51
  • @blz I can ensure you that is not for spamming purposes :). – Milano Jun 28 '15 at 14:56
  • There are few legitimate reasons to cycle through IPs. Either way, it doesn't much matter, because changing IP frequently puts a strain on the Tor network and you're kind of a jerk if you do it. Please reconsider your methods. – Louis Thibault Jun 28 '15 at 14:58

1 Answers1

1

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)
Louis Thibault
  • 16,122
  • 21
  • 72
  • 136