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

dMc*_*ish 8 python google-app-engine reddit

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

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

使用应用程序引擎的交互式shell时,您可以看到错误,其中包含以下代码.

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

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

>>> 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
Run Code Online (Sandbox Code Playgroud)

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

print urllib2.urlopen('http://www.reddit.com/r/Music/.json').read()
Run Code Online (Sandbox Code Playgroud)

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

小智 13

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

用户代理:/ u/spladug超级快乐的天赋机器人

有关reddit api的更多信息,请访问https://github.com/reddit/reddit/wiki/API.