Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从Google应用程序引擎调用Reddit api时出现错误429_Python_Google App Engine_Reddit - Fatal编程技术网

Python 从Google应用程序引擎调用Reddit api时出现错误429

Python 从Google应用程序引擎调用Reddit api时出现错误429,python,google-app-engine,reddit,Python,Google App Engine,Reddit,我已经在Google App Engine上运行了一个多月的cron作业,没有任何问题。该作业可以做很多事情,其中之一是它使用urllib2进行调用,从Reddit以及其他一些站点检索json响应。大约两周前,我开始在调用Reddit时看到错误,但在调用其他站点时没有错误。我收到的错误是HTTP错误429 我尝试过在谷歌应用程序引擎之外执行相同的代码,没有任何问题。我尝试使用urlFetch,但收到相同的错误 您可以在使用以下代码的应用程序时看到错误 import urllib2 data =

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

我尝试过在谷歌应用程序引擎之外执行相同的代码,没有任何问题。我尝试使用urlFetch,但收到相同的错误

您可以在使用以下代码的应用程序时看到错误

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
起初,我认为它与超时问题有关,因为它最初是工作的,但由于没有超时错误,而是奇怪的HttpError代码,我不确定。
有什么想法吗

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


如果您使用Reddit API密钥(我不知道他们是否发出了密钥),或者他们同意根据应用程序头对API调用进行速率限制,那么这可能会更好

redditrate对pythonshell的默认用户代理的api限制非常严格。您需要使用reddit用户名设置一个唯一的用户代理,如下所示:

用户代理:超级快乐flair机器人由/u/splaudg创建


有关reddit api的更多信息,请参见此处。

使用交互式shell和您提供的代码对我很有用。这只是意味着您发出了太多的请求。作为一名程序员,你无能为力。为了避免这些错误,通常可以在请求之间设置睡眠@user947240:遵循shadyabhi的建议。这可能有助于多次呼叫,但在第一次呼叫Reddit时失败。RedditAPI站点只声明在30秒内不能对同一url进行多次调用。他们认为来自多个应用程序的所有谷歌应用程序引擎调用来自同一个源,因此阻止了谷歌应用程序引擎上运行的任何应用程序的调用吗?我添加了重试逻辑和31秒的睡眠。它似乎通常在一两次尝试后起作用,但并不总是如此。我发了reddit的利率限制电子邮件地址,但没有收到回复。Sudhir,这只是猜测。是的。。。是的。我不认为答案是肯定的,这应该是公认的答案。直接从该链接中的文档中可以看出,“许多默认用户代理(如“Python/urllib”或“Java”)受到极大的限制,以鼓励用户代理字符串具有唯一性和描述性。”
print urllib2.urlopen('http://www.reddit.com/r/Music/.json').read()