【发布时间】:2015-10-14 01:50:54
【问题描述】:
还有一个问题看起来和这个问题一样,但事实并非如此。
我有这个代码>
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
【问题讨论】:
-
你能用这个技巧在你的应用程序挂起的地方获取回溯转储并将其添加到问题中:opensourcehacker.com/2015/04/16/…
-
你永远不会打破你的while语句。它不会做任何其他事情。
-
@msanti 我在下面休息一下。但问题是循环在 except 块内。为什么??? IDK
-
可能是它最后的 continue 语句,您是在告诉程序从那时起继续执行 while 循环,下面的任何代码都不会运行,请尝试使用 pass 语句代替继续声明
-
@msanti 谢谢。实际上 continue 语句是正确的。问题是,自从我遇到第一个错误后,我总是会遇到后续错误。该程序挂起,因为我不能使用相对路径作为 br.open 参数,所以它崩溃了。我输入了完整的 HTTP 地址,它成功了。