【发布时间】:2019-01-03 23:56:19
【问题描述】:
我想在 Google App Engine Python 标准运行时环境中使用 requests 模块。
您可以使用第三方库,这些库是纯 Python 代码,没有 C 扩展,将库复制到您的应用程序目录中。如果第三方库已经内置并与运行时捆绑在一起,您可以使用该库而无需将其复制到您的应用中。
第三方库必须以纯 Python 代码的形式实现,没有 C 扩展。如果复制到您的应用程序目录,它们将计入文件配额,因为该库与您的应用程序代码一起上传到 App Engine。
requests 没有与 GAE 绑定,所以我按照说明将其添加到我的应用程序文件夹中。
requests 需要一些其他 GAE 不附带的模块,所以我将它们全部添加到我的应用程序文件夹中:
certifichardetidnaurllib3
又出现了一个问题。我的请求发送到Stack Exchange API,它具有https:// 协议。这是错误:
SSLError: HTTPSConnectionPool(host='api.stackexchange.com', port=443): Max retries exceeded with url: /2.2/1?site=stackoverflow (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.",))
ssl 模块内置在 GAE Python 运行时中,所以我将以下内容放在app.yaml 中:
libraries:
- name: webapp2
version: latest
- name: ssl
version: latest
没有用。我得到了和以前一样的错误。我将 SSL 模块文件夹复制到我的应用程序目录中,并在 main.py 中执行了 import ssl,但现在它抛出了一个异常,要求安装另一个模块:
File "/Users/williamqin/Projects/stackpromo/ssl/__init__.py", line 61, in <module>
import _ssl2 # if we can't import it, let the error propagate
ImportError: No module named _ssl2
我在网上搜索了_ssl2 Python 模块,但在任何地方都找不到!
如何正确使用 Google App Engine 中的requests 模块?
【问题讨论】:
-
您是否在 Python 3 中使用 Flex 环境?这些库内置于标准环境中,而不是 Flex 环境中。因此,您需要将其添加到您的项目中,正如您在第二次尝试中所说的那样。你的目录树是什么样子的?
标签: python python-3.x google-app-engine python-3.6 python-module