4

I am trying to use Tor with python and urllib2 and am stuck. The following

print opener.open('http://check.torproject.org/').read()

And

telnet 127.0.0.1 9051

gives me the following error:

514 Authentication Required.

Here is the code I want to use: But I receive the same 514 Authentication Error on the urllib2.urlopen call.

import urllib2
# using TOR !
proxy_support = urllib2.ProxyHandler({"http" : "127.0.0.1:9051"} )
opener = urllib2.build_opener(proxy_support)
urllib2.install_opener(opener)
# every urlopen connection will then use the TOR proxy like this one :
urllib2.urlopen('http://www.google.com').read()

Any suggestions on why this is occurring?

The Tor Vidalia browser -> settings -> Advanced: Authentication set to 'Randomly Generate'

I am Using Python 2.65 urllib2 Tor

joaquin
  • 82,968
  • 29
  • 138
  • 152
Jim Noss
  • 43
  • 1
  • 6

1 Answers1

1

Google search suggests (and Tor manual confirms) that 9051 is Tor's default control port. Actual proxy is running on port 9050 by default, which is the one you need to use. However, Vidalia does not use the default ports without extra configuration.

The other problem is that urllib2 is by default unable to work with SOCKS proxies. For possible solutions see these two questions.

Community
  • 1
  • 1
Tanriol
  • 1,184
  • 1
  • 7
  • 12
  • So I replaced 9051 with 9050 and now receive 'connection refused.' I am using the Tor browser bundle install for linux. Tor is running via the Vidalia console. The log file shows 'opening control listener on 127.0.0.1:9051.' DO I need to enable the 9050 port? When I do a 'netstat -a|grep 905' all I see is the 9051 port. It must be something simple that I am missing. – Jim Noss Dec 18 '11 at 09:21
  • I found out how to enable to socks port. in the Tor ./Data/Tor/torrc file, I changed SocksPort from auto to 9050. So now I see 9050 listening when I do a netstat. Now the next issue is that when using 9050, urllib2 complains saying that 'Tor is not an HTTP Proxy.' So it now looks like I have to use an HTTP Proxy, like privoxy to work with Tor. If not, please let me know. – Jim Noss Dec 18 '11 at 10:36
  • Looks like Vidalia in the bundle uses non-default ports... Message log - Advanced in Vidalia should contain a line like "Socks listener listening on port " - this is the port you actually need to use. Or switch the port as you've done. – Tanriol Dec 18 '11 at 10:41
  • Yes, Tor itself does not provide an HTTP proxy. – Tanriol Dec 18 '11 at 10:43
  • 1
    Got It! Thanks for the guidance. With Tor's socket listener port setup to be 9050 versus auto, I installed TorCtl which establishes a listening port of 8118 which talks to Tors 9050 port. so my working ProxyHandler line is now: proxy_support = urllib2.ProxyHandler({"http" : "127.0.0.1:8118"}) So I have Python -> urllib2 -> TorCtl -> Tor as the path. – Jim Noss Dec 18 '11 at 18:08
  • Follow up. The following Ubuntu link is very useful. https://help.ubuntu.com/community/Tor – Jim Noss Dec 18 '11 at 23:05