5

I've been trying to get Tor to work with Python, but I've been hitting a brick wall. I simply can't get any of the examples to work. Here is one from Stackoverflow

import urllib2
proxy  = urllib2.ProxyHandler({'http':'127.0.0.1:8118'})
opener = urllib2.build_opener(proxy)
print opener.open('http://check.torproject.org/').read()

I've installed Tor and it works fine while browsing through Aurora. However running this python script I get

Traceback (most recent call last):
File "/home/x/Tor.py", line 4, in <module>
  print opener.open('http://check.torproject.org/').read()
File "/usr/lib/python2.6/urllib2.py", line 391, in open
  response = self._open(req, data)
File "/usr/lib/python2.6/urllib2.py", line 409, in _open
  '_open', req)
File "/usr/lib/python2.6/urllib2.py", line 369, in _call_chain
  result = func(*args)
File "/usr/lib/python2.6/urllib2.py", line 1161, in http_open
  return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib/python2.6/urllib2.py", line 1136, in do_open
  raise URLError(err)
urllib2.URLError: <urlopen error [Errno 111] Connection refused>

I've searched the web, but been unable to find people with simiair problems. Am I missing something totally obvious?!

Community
  • 1
  • 1
Loko
  • 51
  • 1
  • 2

3 Answers3

3

I've written an article showing how to use Tor with Python (using SOCKS) on http://blog.databigbang.com/distributed-scraping-with-multiple-tor-circuits/

Hope it helps.

sw.
  • 3,240
  • 2
  • 33
  • 43
0

I have the same problem but can not find a solution!

I am running Ubuntu, I can open TOR (latest version) with Vidalia, and surf the web correctly. So vidalia works and is connected.

If I use TorCtl in python, I get a response from TOR saying it is live and running!

However, if I want to open a page using urllib2 as described by Loko, I get the same answer.

If someone has a good idea, it would be really nice!

Zist
  • 1
0

Tor acts as a Socks5 proxy. You need to configure your script with that in mind. Google "socks.py"

horus
  • 9