sixohsix的Python Twitter API出现未知错误

4
我有一个小脚本,每小时使用sixohsix's的Twitter Wrapper for Python从API中重复获取推文。我成功处理了来自Twitter API的大多数(如果不是全部)错误,即所有5xx和4xx内容。
然而,我随机地观察到下面的错误追溯(仅在2-3天内出现一次)。我的意思是程序退出并在shell中显示追溯。我不知道这可能意味着什么,但认为它与我的脚本运行大部分时间都正确无关。
这是我在脚本中调用包装器函数的地方:
KW = {
        'count': 200, # number of tweets to fetch (fetch maximum)
        'user_id' : tweeter['user_id'],
        'include_rts': 'false', # do not include native RT's 
        'trim_user' : 'true', 
     }
     timeline = tw.twitter_request(tw_endpoint,\
         tw_endpoint.statuses.user_timeline, KW)

tw.twitter_request(tw_endpoint, tw_endpoint.statuses.user_timeline, KW)函数基本上是执行return tw_endpoint.statuses_user_timeline(**args),其中args被翻译为KW,而tw_endpoint则是使用sixohsix库获取的OAuthorized端点。

return twitter.Twitter(domain='api.twitter.com', api_version='1.1',
                    auth=twitter.oauth.OAuth(access_token, access_token_secret,
                    consumer_key, consumer_secret))

这是追踪信息:

Traceback (most recent call last):
  File "search_twitter_entities.py", line 166, in <module>
    tw_endpoint.statuses.user_timeline, KW)
  File "/home/tg/mild/twitter_utils.py", line 171, in twitter_request
    return twitter_function(**args)
  File "build/bdist.linux-x86_64/egg/twitter/api.py", line 173, in __call__
  File "build/bdist.linux-x86_64/egg/twitter/api.py", line 177, in _handle_response
  File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 400, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 418, in _open
    '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1215, in https_open
    return self.do_open(httplib.HTTPSConnection, req)
  File "/usr/lib/python2.7/urllib2.py", line 1180, in do_open
    r = h.getresponse(buffering=True)
  File "/usr/lib/python2.7/httplib.py", line 1030, in getresponse
    response.begin()
  File "/usr/lib/python2.7/httplib.py", line 407, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python2.7/httplib.py", line 371, in _read_status
    raise BadStatusLine(line)
httplib.BadStatusLine: ''

我从那个追踪回溯中能够得到的唯一信息是:错误发生在另一个Python库的深处,与来自Twitter API或其包装器的无效HTTP状态有关......但正如我所说,也许你们中的一些人可以给我一些提示,告诉我如何调试/解决这个问题,因为经常检查我的脚本并重新启动它以继续获取推文相当烦人。
编辑:为了澄清这一点,追踪回溯中的前两个函数已经位于try-except块中。例如,文件“twitter_utils.py”中的try-except块过滤掉40x和50x异常,但也寻找仅有except:的一般异常。所以我不理解的是为什么错误没有在此位置被捕获,而是程序被强制关闭并打印出回溯?简单地说,我处于无法捕获错误的情况,就像PHP脚本中的解析错误一样。那么我该怎么做呢?
1个回答

0
也许这会指引你朝着正确的方向。这是在调用BadStatusLine时被称为的内容:
class BadStatusLine(HTTPException):
    def __init__(self, line):
        if not line:
            line = repr(line)
        self.args = line,
        self.line = line

我对httplib不是很熟悉,但如果我必须猜测的话,你得到了一个空的响应/错误行,它无法被解析。在你的程序停止的那一行之前有注释:

    # Presumably, the server closed the connection before
    # sending a valid response.
    raise BadStatusLine(line)

如果Twitter在发送响应之前关闭连接,您可以尝试重新连接,也就是在“search_twitter_entities.py”的166行处进行几次try/except操作(虽然有些不太美观)。
try:
     timeline = tw.twitter_request(tw_endpoint,\
              tw_endpoint.statuses.user_timeline, KW)
except:
     try:
          timeline = tw.twitter_request(tw_endpoint,\
                   tw_endpoint.statuses.user_timeline, KW) # try again
     except:
          pass 

或者,假设您每次都可以重新分配时间线为无,那么进行while循环:

timeline = None
while timeline == None:
     try:
          timeline = tw.twitter_request(tw_endpoint,\
              tw_endpoint.statuses.user_timeline, KW)
     except:
          pass

我没有测试过那个。检查一下有没有错误的代码。


其实并不是那么简单。首先,timeline = tw.twitter_request(tw_endpoint, tw_endpoint.statuses.user_timeline, KW) 已经在 try-except 块中,还包括一个 except: 来接受所有异常,因此应该可以捕获所有异常。函数 twitter_request 也是一样。但是,没有任何异常被抛出,程序立即退出并显示 traceback。所以实际问题是:如何在不终止脚本的情况下捕获该错误? - grssnbchr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接