编程语言
首页 > 编程语言> > python – HTTPS使用Google App Engine中的“请求”模块失败

python – HTTPS使用Google App Engine中的“请求”模块失败

作者:互联网

我想在Google App Engine Python标准运行时环境中使用请求模块.

引自official Google Cloud docs

You can use third-party libraries that are pure Python code with no C extensions, by copying the library into your application directory. If the third-party library is already built-in, bundled with the runtime, you can use the library without copying it into your app.

Third party libraries must be implemented as pure Python code with no C extensions. If copied to your application directory, they count towards file quotas because the library is uploaded to App Engine along with your application code.

请求未与GAE捆绑在一起,因此我根据说明将其添加到我的应用程序文件夹中.

请求需要一些其他模块没有GAE,所以我将所有模块添加到我的应用程序文件夹:

>证书
> chardet
>艾达娜
> urllib3

另一个问题出现了.我的请求转到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中导入了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中的请求模块?

解决方法:

在GAE标准中设置python 2.7是一件痛苦的事.它涉及使用app引擎的beta版本的python ssl库以及其他一些可能的结果.

我相信你会遇到python3的一些不同之处.这是让我工作的关键点:

要求2.18.2

requests_toolbelt 0.7.0

在appengine_config.py中执行此操作:

from requests_toolbelt.adapters import appengine as requests_toolbelt_appengine

# Use the App Engine Requests adapter. This makes sure that Requests uses
# URLFetch.
requests_toolbelt_appengine.monkeypatch()

在app.yaml中有这些:

env_variables:
  GAE_USE_SOCKETS_HTTPLIB : 'true'


libraries:
- name: ssl
  version: "2.7.11"
- name: pycrypto
  version: "2.6"

FUTHERMORE,这让我在制作中工作,但不是在我的本地主机上.除了包含我所有第三方库的本地lib目录之外,我还必须设置一个名为localhost_libs的附加目录,并在appengine_config.py中执行此操作:

vendor.add('lib')

if os.environ.get('SERVER_SOFTWARE', '').startswith('Development'):
    vendor.add('localhost_libs')

我有pycrypto的地方

此外,很长一段时间,每个人都必须这样做(最终在dev_appserver中发生了一些改变,使其无法工作):“ImportError: No module named _ssl” with dev_appserver.py from Google App Engine

标签:python-3-6,python,python-3-x,google-app-engine,python-module
来源: https://codeday.me/bug/20191007/1867668.html