georgesilva
georgesilva

Reputation: 29

Mechanize br.open hangs indefinetly

Theres another question that looks the same as this one, but its not.

I have this code>

    while True:
        iterator = iterator+1
        try:
            response = br.open('/cgi-bin/bet.pl?*********')
            tried = 0
        except :
            tried += 1
            print "Request Timed Out occurred "+str(tried)+" times. Waiting a few moments before try again."
            time.sleep(4)
            continue

When I try to test it, closing the connection it hangs on the response line. Like forever. When I restabilish the internet, it stills hangs. And when I press Ctrl-C twice it goes to the except block. But it hangs again, it keeps printing the error message on a infinite loop. I have to press Ctrl-C a third time to program stops. What have I been doing wrong? I have tried several solutions, but nothing.

EDIT:

Is this helpfull?

    > File "build/bdist.linux-x86_64/egg/mechanize/_mechanize.py", line 203, in open
    File "build/bdist.linux-x86_64/egg/mechanize/_mechanize.py", line 230, in _mech_open
    File "build/bdist.linux-x86_64/egg/mechanize/_opener.py", line 193, in open
    File "build/bdist.linux-x86_64/egg/mechanize/_urllib2_fork.py", line 344, in _open
    File "build/bdist.linux-x86_64/egg/mechanize/_urllib2_fork.py", line 332, in _call_chain
    File "build/bdist.linux-x86_64/egg/mechanize/_urllib2_fork.py", line 1170, in https_open
    File "build/bdist.linux-x86_64/egg/mechanize/_urllib2_fork.py", line 1115, in do_open
    File "/usr/lib/python2.7/httplib.py", line 979, in request
self._send_request(method, url, body, headers)
    File "/usr/lib/python2.7/httplib.py", line 1013, in _send_request
self.endheaders(body)
    File "/usr/lib/python2.7/httplib.py", line 975, in endheaders
self._send_output(message_body)
    File "/usr/lib/python2.7/httplib.py", line 835, in _send_output
self.send(msg)
    File "/usr/lib/python2.7/httplib.py", line 797, in send
self.connect()
    File "/usr/lib/python2.7/httplib.py", line 1182, in connect
self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file)
    File "/usr/lib/python2.7/ssl.py", line 487, in wrap_socket
ciphers=ciphers)
    File "/usr/lib/python2.7/ssl.py", line 243, in __init__
self.do_handshake()
    File "/usr/lib/python2.7/ssl.py", line 405, in do_handshake
self._sslobj.do_handshake()

SOLUTION: First you change the relative path to absolute path in br.open argument. then... Its a problem with urllib. I chaged import urllib to import urllib2 and voilá!!! Now the code looks like this: try: response = br.open('http://*****/cgi-bin/bet.pl?') except urllib2.URLError as e: print "URLError................." time.sleep(4) continue

Upvotes: 1

Views: 521

Answers (1)

georgesilva
georgesilva

Reputation: 29

As @msanti mentioned, the address I was using as br.open argument could be wrong. So I changed, instead of a relative path to cgi-bin/bet.pl I put the complete address and it worked. The reason the open method was working before I got the first urlException is a mistery to me.

Just posting in case anyone is facing the same problem.

Upvotes: 1

Related Questions