Python脚本奇怪的问题

时间:2012-08-11 10:37:34

标签: python urllib2 httplib

嘿,我正在使用python脚本来测试网络商务网站上的功能。我把这个脚本放在pythonanywhere网站上,有时连续工作7天没有问题,但有几天,比如这个,当我收到这个错误时:

Logged in with test_cart
    Open http://[***]/electronics/page/1
Search ... iphone
[****************]
    File "/home/.../cart-pickup.py", line 58, in alo
    site = opener.open(url)
    File "/usr/local/lib/python2.7/urllib2.py", line 400, in open
    response = self._open(req, data)
    File "/usr/local/lib/python2.7/urllib2.py", line 418, in _open
    '_open', req)
    File "/usr/local/lib/python2.7/urllib2.py", line 378, in _call_chain
    result = func(*args)
    File "/usr/local/lib/python2.7/urllib2.py", line 1207, in http_open
    return self.do_open(httplib.HTTPConnection, req)
    File "/usr/local/lib/python2.7/urllib2.py", line 1180, in do_open
    r = h.getresponse(buffering=True)
    File "/usr/local/lib/python2.7/httplib.py", line 1030, in getresponse
    response.begin()
    File "/usr/local/lib/python2.7/httplib.py", line 407, in begin
    version, status, reason = self._read_status()
    File "/usr/local/lib/python2.7/httplib.py", line 371, in _read_status
    raise BadStatusLine(line)
    httplib.BadStatusLine: ''
    >>>         

任何想法可能是什么? 提前致谢

1 个答案:

答案 0 :(得分:3)

您可以使用try..except

教您的程序如何从这种情况中恢复而不会崩溃
try:
    site = opener.open(url)  # line 58 in cart-pickup.py
except IOError: 
    print "Error: Site not loading or it isnt UP. Retrying..." 
    # return site # This will cause a NameError if site is not defined.
    return url    # Perhaps you meant this?
except httplib.BadStatusLine:
    # log opening `url` did not succeed, or
    # queue it to be tried again, etc.
    pass