【发布时间】:2015-04-02 15:19:00
【问题描述】:
我在 Google App Engine 中使用 python 的 requests 库将 GET 请求发送到私有服务器。当我提出请求时,我会收到以下警告:
requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning
根据指向的documentation,我需要升级到 GAE 使用的 Python 2.7.x,或者使用 pyopenssl。因为我不相信我可以强制 GAE 使用 Python 2.7.9,所以我尝试使用 pyopenssl。
按照页面上的说明,我已将建议的三个库下载到我的应用程序的 lib 目录中,并在我使用请求的地方尝试将 pyopenssl 注入 urllib3:
import requests.packages.urllib3.contrib.pyopenssl
requests.packages.urllib3.contrib.pyopenssl.inject_into_urllib3()
但是,这在开发服务器和生产服务器中失败,并带有以下回溯:
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
File "/base/data/home/apps/s~servicey1564/1.383321878696068897/main.py", line 24, in <module>
from API import setupautomatorAPI
File "/base/data/home/apps/s~servicey1564/1.383321878696068897/API.py", line 12, in <module>
from ServiceActivationTest import uploadSAT, getSATsForService
File "/base/data/home/apps/s~servicey1564/1.383321878696068897/ServiceActivationTest/__init__.py", line 3, in <module>
from requests.packages.urllib3.contrib import pyopenssl as pyopenssl
File "/base/data/home/apps/s~servicey1564/1.383321878696068897/lib/requests/packages/__init__.py", line 95, in load_module
raise ImportError("No module named '%s'" % (name,))
ImportError: No module named 'requests.packages.urllib3.contrib.pyopenssl'
这个 import 语句在 Python 解释器中运行良好,如果我最后关闭 pyopenssl 也可以运行。 pyopenssl 也是该路径中除 __init__.py 文件之外的第一个 .py 文件。
我在这里做错了吗?有没有更简单的方法来修复 InsecurePlatformWarning?
更新:转到套接字 API 页面后(感谢 shazow!)我发现我的部分问题是 httplib 行为不端,因为我缺少环境变量。这并没有消除警告,但我的证书现在正在被接受!
【问题讨论】:
-
嗯,我还没有完整的答案,但有一些提示:我不相信 PyOpenSSL 可以在 AppEngine 上运行,因为它是一个编译模块。 AppEngine 提供了自己的版本。也许您需要启用
socketsAPI 以获得正确的 SSLContext? cloud.google.com/appengine/docs/python/sockets -
我相信我启用了套接字。我的理解是,如果启用计费,套接字将自动可用。
标签: python google-app-engine ssl python-requests urllib3