1

This could be a very silly question, but I'm new to this topic and I couldn't figure out the problem after searching for a long time.

It seems like I do have a Tor process listening. I tested it by using telnet 127.0.0.1 9150, and it works:

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

I would like to use the mechanize package in Python. Here are my codes:

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", 9150)

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

import mechanize
from mechanize import Browser

br = Browser()
print br.open('http://icanhazip.com').read()

I'm not sure how to move forward based on the following traceback:

Traceback (most recent call last):
  File "/Users/Documents/webscraping/BeautifulSoup/temp1.py", line 18, in <module>
    print br.open('http://icanhazip.com').read()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mechanize/_mechanize.py", line 203, in open
    return self._mech_open(url, data, timeout=timeout)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mechanize/_mechanize.py", line 230, in _mech_open
    response = UserAgentBase.open(self, request, data)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mechanize/_opener.py", line 188, in open
    req = meth(req)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mechanize/_http.py", line 316, in http_request
    self.rfp.read()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mechanize/_http.py", line 242, in read
    f = self._opener.open(req)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mechanize/_mechanize.py", line 203, in open
    return self._mech_open(url, data, timeout=timeout)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mechanize/_mechanize.py", line 230, in _mech_open
    response = UserAgentBase.open(self, request, data)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mechanize/_opener.py", line 193, in open
    response = urlopen(self, req, data)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mechanize/_urllib2_fork.py", line 344, in _open
    '_open', req)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mechanize/_urllib2_fork.py", line 332, in _call_chain
    result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mechanize/_urllib2_fork.py", line 1142, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mechanize/_urllib2_fork.py", line 1115, in do_open
    h.request(req.get_method(), req.get_selector(), req.data, headers)
  File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/httplib.py", line 958, in request
    self._send_request(method, url, body, headers)
  File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/httplib.py", line 992, in _send_request
    self.endheaders(body)
  File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/httplib.py", line 954, in endheaders
    self._send_output(message_body)
  File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/httplib.py", line 814, in _send_output
    self.send(msg)
  File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/httplib.py", line 776, in send
    self.connect()
  File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/httplib.py", line 757, in connect
    self.timeout, self.source_address)
  File "/Users/ywang/Documents/webscraping/BeautifulSoup/temp1.py", line 5, in create_connection
    sock.connect(address)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/socks.py", line 392, in connect
    self.__negotiatesocks5(destpair[0],destpair[1])
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/socks.py", line 199, in __negotiatesocks5
    self.sendall("\x05\x01\x00")
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/socks.py", line 165, in sendall
    socket.socket.sendall(self, bytes)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/socks.py", line 165, in sendall
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/socks.py", line 165, in sendall
    socket.socket.sendall(self, bytes)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/socks.py", line 163, in sendall
    if 'encode' in dir(bytes):
RuntimeError: maximum recursion depth exceeded while calling a Python object

I would be very grateful if someone could help me out!

  • 1
    Problem solved. My mistake is that I should not have installed SocksiPy by using sudo apt-get install python-socksipy. Instead, the correct way to do is to download socks.py from socksipy.sourceforge.net and copy it to the python's site package folder. Thanks! – user3059 Jul 6 '14 at 0:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.