App Engine: Alternatives to urlfetch? Seems very unreliable

情到浓时终转凉″ 提交于 2019-12-01 16:47:25
systempuntoout

Sometimes it'll work as it should, but other times completely randomly with no code changes

This is a common symptom that your application's requests have exceeded the Yahoo API calls rate limit.

Quoting Yahoo developer documentations rate limit:

IP Based Limits

Our service rate limits are imposed as a limit on the number of API calls made per IP address during a specific time window. If your IP address changes during that time period, you may find yourself with more "credit" available. However, if someone else had been using the address and hit the limit, you'll need to wait until the end of the time period to be allowed to make more API calls.

Google App Engine uses a pool of IP addresses for outgoing urlfetch requests and your application is sharing these IP addresses with other applications that are calling the same Yahoo endpoint; when the rate limit is exceeded, the endpoint replies with a limit exceeded error causing UrlFetch to fail.
Here another case using the Twitter search API.

When you mix Google App Engine+Third party web APIs, you need to be sure that the API provides authenticated calls allowing your application to have its own quota (StackApps API for example).

import urllib2
response = urllib2.urlopen('http://python.org/')
html = response.read()

This isn't an error in URLFetch - it's an issue with the JSON being returned. Either json.loads is returning None, or r['query'] is - I'm guessing it's probably the latter. Try logging result.content to see what the service is returning. You probably also want to cehck result.status.

One possibility is that your request is being denied or ratelimited by Yahoo in production, but not on your development machine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!