以下代码适用于 Python 交互式 shell:
import urllib2
result = urllib2.urlopen("http://www.google.com/")
并给出 200 个结果。
如果我在使用开发服务器在本地运行的 AppEngine 应用程序中运行相同的代码,它会失败并出现以下错误:
URLError: <urlopen error An error occured while connecting to the server:
Unable to fetch URL: http://www.google.com/
Error: [Errno 11004] getaddrinfo failed>`
我试过直接使用 urlfetch
库:
from google.appengine.api import urlfetch
result = urlfetch.fetch("http://www.google.com")
这也失败了(这是有道理的,因为我相信 AppEngine 中的 urllib2
在内部调用 URLFetch
?)
我可以从我的本地机器清楚地访问该 URL - 那么发生了什么?
更新:相关堆栈跟踪:
File "c:\dev\repos\stackoverflow\main.py", line 40, in get_latest_comments
result = urlfetch.fetch("http://www.google.com")
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\urlfetch.py", line 266, in fetch
return rpc.get_result()
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\apiproxy_stub_map.py", line 604, in get_result
return self.__get_result_hook(self)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\urlfetch.py", line 397, in _get_fetch_result
raise DownloadError("Unable to fetch URL: " + url + error_detail)
DownloadError: Unable to fetch URL: http://www.google.com Error: [Errno 11004] getaddrinfo failed
最佳答案
你有配置环境变量的代理吗? dev_appserver 清除所有环境变量。
关于python - 无法使用 urlfetch 或 urllib2 从 App Engine 开发服务器连接到外部服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12566583/