从Google App Engine调用Reddit api时出现错误429

时间:2012-01-22 18:29:53

标签: python google-app-engine reddit

我一直在Google App Engine上运行一个cron工作一个多月了,没有任何问题。这项工作做了很多事情,一个是它使用urllib2来调用从Reddit以及其他一些站点检索json响应。大约两周前,我开始在调用Reddit时看到错误,但在调用其他站点时没有错误。我收到的错误是HTTP错误429。

我尝试在Google App Engine之外执行相同的代码,但没有任何问题。我尝试使用urlFetch,但收到同样的错误。

使用带有以下代码的应用engine's interactive shell时,您可以看到错误。

import urllib2
data = urllib2.urlopen('http://www.reddit.com/r/Music/.json', timeout=60)

编辑:不确定为什么它总是对我而不是其他人失败。这是我收到的错误:

>>> import urllib2
>>> data = urllib2.urlopen('http://www.reddit.com/r/Music/.json', timeout=60)
Traceback (most recent call last):
  File "/base/data/home/apps/s~shell-27/1.356011914885973647/shell.py", line 267, in get
    exec compiled in statement_module.__dict__
  File "<string>", line 1, in <module>
  File "/base/python27_runtime/python27_dist/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/base/python27_runtime/python27_dist/lib/python2.7/urllib2.py", line 400, in open
    response = meth(req, response)
  File "/base/python27_runtime/python27_dist/lib/python2.7/urllib2.py", line 513, in http_response
    'http', request, response, code, msg, hdrs)
  File "/base/python27_runtime/python27_dist/lib/python2.7/urllib2.py", line 438, in error
    return self._call_chain(*args)
  File "/base/python27_runtime/python27_dist/lib/python2.7/urllib2.py", line 372, in _call_chain
    result = func(*args)
  File "/base/python27_runtime/python27_dist/lib/python2.7/urllib2.py", line 521, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 429: Unknown

在app引擎外部运行的类似代码没有问题:

print urllib2.urlopen('http://www.reddit.com/r/Music/.json').read()

起初我认为它与超时问题有关,因为它最初工作,但由于没有超时错误而是一个奇怪的HttpError代码,我不确定。 有任何想法吗?

2 个答案:

答案 0 :(得分:13)

Reddit速率对于python shell的默认用户代理非常严格地限制了api。您需要在其中设置一个唯一的用户代理,其中包含您的reddit用户名,如下所示:

  

User-Agent:/ u / spladug

的超级快乐天赋机器人

有关reddit api https://github.com/reddit/reddit/wiki/API的更多信息。

答案 1 :(得分:0)

Reddit可能正在计算基于IP的呼叫 - 这意味着GAE上与您共享IP的其他应用程序可能已经耗尽了配额。

如果您使用Reddit API密钥(我不知道他们是否发布)或者他们是否同意根据应用程序标头对API限制进行评级,这可能会更好。