机械化br.open坚定地挂起

时间:2015-10-14 01:50:55

标签: python mechanize-python

另一个看起来和这个问题相同的问题,但不是。

我有这段代码>

    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

当我尝试测试时,关闭连接会挂起响应行。就像永远。 当我重新安排互联网时,它仍然悬而未决。当我按两次Ctrl-C时,它会转到except块。但它再次挂起,它继续在无限循环上打印错误消息。我必须第三次按Ctrl-C来编程停止。 我做错了什么?我尝试了几种解决方案,但没有。

编辑:

这有用吗?

    > 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()

解: 首先,在br.open参数中更改绝对路径的相对路径。然后... 这是urllib的一个问题。我将import urllib改为import urllib2并且瞧! 现在代码看起来像这样: try: response = br.open('http://*****/cgi-bin/bet.pl?') except urllib2.URLError as e: print "URLError................." time.sleep(4) continue

1 个答案:

答案 0 :(得分:1)

正如@msanti所提到的,我用作br.open参数的地址可能是错误的。所以我改变了,而不是cgi-bin / bet.pl的相对路径,我把完整的地址和它工作。 在我得到第一个urlException之前open方法正常工作的原因对我来说是一个谜。

只是发布以防任何人遇到同样的问题。