python - 无法使用 urlfetch 或 urllib2 从 App Engine 开发服务器连接到外部服务

标签 python google-app-engine urllib2 urlfetch

以下代码适用于 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/

相关文章:

python - 在 python 中的列表 VS 元组条件中

google-app-engine - 建立实体组,同时保持对长 ID 的访问

android - SSLHandshakeException 云端点客户端库

python - 如何将这些 header 添加到我的 python urllib opener?

python - URLlib 未显示在谷歌分析中

python - 在横向连接 SQLAlchemy 中添加计算列

python - 在 python + flask + gunicorn + nginx + Compute Engine 应用程序中从 Google Cloud Storage 读取文件失败

python - 更改行数据以防它与不同的行不匹配

java - 如何创建视频的缩略图?

python - 与 urllib2 或其他 http 库的多个(异步)连接?