【问题标题】:Unknown error in sixohsix's Twitter API for PythonSixohsix 的 Python 的 Twitter API 中的未知错误
【发布时间】:2013-01-05 10:01:28
【问题描述】:

我有一个小脚本,使用 Sixohsix 重复(每小时)从 API 获取推文 Twitter Wrapper for Python。我成功地处理了来自 Twitter API 的大部分(如果不是全部)错误,即所有 5xx 和 4xx 的东西。

尽管如此,我还是随机观察到以下错误回溯(2-3 天只有一次)。我的意思是程序退出并且回溯显示在外壳中。我不知道这可能意味着什么,但认为它与我的脚本的作用没有直接关系,因为它已证明自己在大多数情况下都能正确运行。

这是我在脚本中调用包装器函数的地方:

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-Block 过滤掉 40x 和 50x 异常,但也会查找只有 except: 的一般异常。所以我不明白为什么错误没有在这个位置被捕获,而是程序被强制关闭并打印回溯?简而言之,我处于无法捕获错误的情况,就像 PHP 脚本中的解析错误一样。那我该怎么做呢?

【问题讨论】:

    标签: python python-2.7 twitter httplib


    【解决方案1】:

    也许这会为您指明正确的方向。这是调用 BadStatusLine 时调用的内容:

    class BadStatusLine(HTTPException):
        def __init__(self, line):
            if not line:
                line = repr(line)
            self.args = line,
            self.line = line
    

    我对 httplib 不太熟悉,但如果我不得不猜测,您会得到一个空的响应/错误行,而且,它无法被解析。在您的程序停止的行之前有 cmets:

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

    如果 twitter 在发送响应之前关闭连接,您可以再试一次,这意味着在“search_twitter_entities.py”第 166 行尝试/例外几次(丑陋)。

    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 相同。尽管如此,没有抛出异常,程序立即退出,并带有回溯。所以实际的问题是:如何在不终止脚本的情况下捕获该错误?
    猜你喜欢
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    • 2012-03-15
    • 1970-01-01
    • 2019-07-21
    • 2014-04-03
    相关资源
    最近更新 更多