I have the following code:

with Controller.from_port(port = 9151) as controller:

                    socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "", 9150)
                    socket.socket = socks.socksocket

                    url= "https://www.example.com"

                    browser = mechanize.Browser()

                    text = browser.open(url).read()

When I run it I get the following lengthy traceback:

Traceback (most recent call last):
      File "C:\Users\HOJU\Desktop\edp\gog.py", line 168, in <module>
      File "C:\Users\HOJU\Desktop\edp\gog.py", line 101, in main
        text = browser.open(url).read()
      File "C:\Python27\lib\site-packages\mechanize\_mechanize.py", line 203, in      ope
    return self._mech_open(url, data, timeout=timeout)
  File "C:\Python27\lib\site-packages\mechanize\_mechanize.py", line 230, in _me
    response = UserAgentBase.open(self, request, data)
  File "C:\Python27\lib\site-packages\mechanize\_opener.py", line 193, in open
    response = urlopen(self, req, data)
  File "C:\Python27\lib\site-packages\mechanize\_urllib2_fork.py", line 344, in
    '_open', req)
  File "C:\Python27\lib\site-packages\mechanize\_urllib2_fork.py", line 332, in
    result = func(*args)
  File "C:\Python27\lib\site-packages\mechanize\_urllib2_fork.py", line 1170, in
    return self.do_open(conn_factory, req)
  File "C:\Python27\lib\site-packages\mechanize\_urllib2_fork.py", line 1115, in
    h.request(req.get_method(), req.get_selector(), req.data, headers)
  File "C:\Python27\lib\httplib.py", line 946, in request
    self._send_request(method, url, body, headers)
  File "C:\Python27\lib\httplib.py", line 987, in _send_request
  File "C:\Python27\lib\httplib.py", line 940, in endheaders
  File "C:\Python27\lib\httplib.py", line 803, in _send_output
  File "C:\Python27\lib\httplib.py", line 755, in send
  File "C:\Python27\lib\httplib.py", line 1152, in connect
    self.timeout, self.source_address)
  File "C:\Users\HOJU\Desktop\edp\gog.py", line 32, in create_connection
  File "C:\Python27\lib\site-packages\socks.py", line 392, in connect
  File "C:\Python27\lib\site-packages\socks.py", line 199, in __negotiatesocks5
  File "C:\Python27\lib\site-packages\socks.py", line 165, in sendall
    socket.socket.sendall(self, bytes)
  File "C:\Python27\lib\site-packages\socks.py", line 165, in sendall
    socket.socket.sendall(self, bytes)
  File "C:\Python27\lib\site-packages\socks.py", line 165, in sendall
    socket.socket.sendall(self, bytes)                     
  File "C:\Python27\lib\site-packages\socks.py", line 165, in sendall
    socket.socket.sendall(self, bytes)
  File "C:\Python27\lib\site-packages\socks.py", line 165, in sendall
    socket.socket.sendall(self, bytes)
  File "C:\Python27\lib\site-packages\socks.py", line 165, in sendall
    socket.socket.sendall(self, bytes)
  File "C:\Python27\lib\site-packages\socks.py", line 163, in sendall
    if 'encode' in dir(bytes):
RuntimeError: maximum recursion depth exceeded while calling a Python object

How can I fix this?

  • Do you use the Stem library?
    – Jens Kubieziel
    Commented Apr 2, 2016 at 20:36
  • yes i do use the stem library. i was actually able to resolve the above issue . now the programs runs properly. However when it opens the url for the google page that i am trying to read i get a 503 error, do you know why this happens? @JensKubieziel Commented Apr 2, 2016 at 20:48
  • @JensKubieziel does google automatically block a request if it sees that it comes from a Tor network? Commented Apr 2, 2016 at 20:49
  • @abcla No it does not blocks a request from Tor on automatical basis
    – Alexey Vesnin
    Commented Apr 2, 2016 at 23:38
  • @abcla please answer your question and describe what you did to resolve your issue. This helps other people which might run into the same problem.
    – Jens Kubieziel
    Commented Apr 3, 2016 at 9:04

1 Answer 1


Here are your pyCURL and a howto

UPDATE: Copy-pasting by request - copyright to code to sources mentioned upstrings:

amnesia@amnesia: ~$ cat checkTor.py
#!/usr/bin/env python

import pycurl

curl = pycurl.Curl()
curl.setopt( pycurl.URL, 'https://check.torproject.org/' )
curl.setopt( pycurl.PROXY, '' )
curl.setopt( pycurl.PROXYPORT, 9050 )
curl.setopt( pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS5_HOSTNAME )

amnesia@amnesia: ~$ time ./checkTor.py
<!doctype html>
Congratulations. This browser is configured to use Tor.
real 0m2.487s
user 0m0.060s
sys 0m0.000s

That is your HTTPS request via Tor, no DNS leaks. Your STEM request must not use proxy - Tor does not allow LAN IP's through SOCKS, and it's correct. You must not use any proxies sending signal NEWNYM. To capture HTML output use cURL options like RETURN_TRANSFER - see docs to fit your needs

  • Your answer is not really helpful as it stands. Please describe a specific solution to the question. Links to other sites might disappear.
    – Jens Kubieziel
    Commented Apr 3, 2016 at 9:02
  • @JensKubieziel would you please be so kind reading the links provided before complaining about the answer? If the links to the cURL for Python official site will be dead - then pycurl itself will be dead and an answer will be useles even if there will be a full copy-paste. Or should I attach an official repository source code snapshot too? For a case, you know? This is really ridiculous complain, man! No offense - just check the links and smile =)
    – Alexey Vesnin
    Commented Apr 3, 2016 at 14:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .