Python SSLError:无法连接到 HTTPS URL,因为 SSL 模块在 Google 应用引擎上不可用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30381581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:20:52  来源:igfitidea点击:

SSLError: Can't connect to HTTPS URL because the SSL module is not available on google app engine

pythongoogle-app-enginesslpython-requestswechat

提问by Nosakhare Belvi

Want to use wechat sdkto create menu

想用微信sdk做菜单

WeChat.create_menu({
     "button":[
     {    
          "type":"click",
          "name":"Daily Song",
          "key":"V1001_TODAY_MUSIC"
      },
      {
           "type":"click",
           "name":" Artist Profile",
           "key":"V1001_TODAY_SINGER"
      },
      {
           "name":"Menu",
           "sub_button":[
           {    
               "type":"view",
               "name":"Search",
               "url":"http://www.soso.com/"
            },
            {
               "type":"view",
               "name":"Video",
               "url":"http://v.qq.com/"
            },
            {
               "type":"click",
               "name":"Like us",
               "key":"V1001_GOOD"
            }]
       }]
 })

Currently not work because of this error:

由于此错误,目前无法正常工作:

Traceback (most recent call last):
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 267, in Handle
    result = handler(dict(self._environ), self._StartResponse)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1519, in __call__
    response = self._internal_error(e)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1511, in __call__
    rv = self.handle_exception(request, response, e)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1505, in __call__
    rv = self.router.dispatch(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1077, in __call__
    return handler.dispatch()
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 547, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 545, in dispatch
    return method(*args, **kwargs)
  File "/base/data/home/apps/s~project-boom/1.384461758981660124/wechatAPIHandler.py", line 72, in post
    "key":"V1001_GOOD"
  File "/base/data/home/apps/s~project-boom/1.384461758981660124/wechat_sdk/basic.py", line 355, in create_menu
    data=menu_data
  File "/base/data/home/apps/s~project-boom/1.384461758981660124/wechat_sdk/basic.py", line 949, in _post
    **kwargs
  File "/base/data/home/apps/s~project-boom/1.384461758981660124/wechat_sdk/basic.py", line 907, in _request
    "access_token": self.access_token,
  File "/base/data/home/apps/s~project-boom/1.384461758981660124/wechat_sdk/basic.py", line 849, in access_token
    self.grant_token()
  File "/base/data/home/apps/s~project-boom/1.384461758981660124/wechat_sdk/basic.py", line 273, in grant_token
    "secret": self.__appsecret,
  File "/base/data/home/apps/s~project-boom/1.384461758981660124/wechat_sdk/basic.py", line 935, in _get
    **kwargs
  File "/base/data/home/apps/s~project-boom/1.384461758981660124/wechat_sdk/basic.py", line 917, in _request
    **kwargs
  File "/base/data/home/apps/s~project-boom/1.384461758981660124/requests/api.py", line 50, in request
    response = session.request(method=method, url=url, **kwargs)
  File "/base/data/home/apps/s~project-boom/1.384461758981660124/requests/sessions.py", line 465, in request
    resp = self.send(prep, **send_kwargs)
  File "/base/data/home/apps/s~project-boom/1.384461758981660124/requests/sessions.py", line 573, in send
    r = adapter.send(request, **kwargs)
  File "/base/data/home/apps/s~project-boom/1.384461758981660124/requests/adapters.py", line 431, in send
    raise SSLError(e, request=request)
SSLError: Can't connect to HTTPS URL because the SSL module is not available.

python request module is include in the app engine project. Using python 2.7. Being look for ways to solve this problem but have not find very clear way to solve the problem yet

python请求模块包含在应用引擎项目中。使用 python 2.7。正在寻找解决此问题的方法但尚未找到解决问题的非常明确的方法

采纳答案by Yahel

If you're using GAE's Sockets, you can get SSL support without any hacks by simply loading the SSL library.

如果您使用 GAE 的 Sockets,您可以通过简单地加载 SSL 库来获得 SSL 支持而无需任何黑客攻击。

Simply add this to your app.yaml file:

只需将其添加到您的 app.yaml 文件中:

libraries:
- name: ssl
  version: latest

This is documented on Google Cloud's OpenSSL Support documentation.

这记录在Google Cloud 的 OpenSSL 支持文档中。

回答by ezra-g

This blog post details a solution. From the blog post:

这篇博文详细介绍了一个解决方案。来自博客文章:

The problem is GAE has a “whitelist” of select standard libraries. SSL (_ssl, _socket) is not one of them. So, we need to tweak the sandbox environment (dangerous) carefully. The below code uses the standard Python socket library instead of the GAE-provided in the development environment. Modify [or create] appengine_config.py:

问题是 GAE 有一个选择标准库的“白名单”。SSL (_ssl, _socket) 不是其中之一。因此,我们需要仔细调整沙箱环境(危险)。下面的代码使用标准的 Python 套接字库,而不是开发环境中提供的 GAE。修改 [或创建] appengine_config.py:

import os

# Workaround the dev-environment SSL
#   http://stackoverflow.com/q/16192916/893652
if os.environ.get('SERVER_SOFTWARE', '').startswith('Development'):
    import imp
    import os.path
    from google.appengine.tools.devappserver2.python import sandbox

    sandbox._WHITE_LIST_C_MODULES += ['_ssl', '_socket']
    # Use the system socket.
    psocket = os.path.join(os.path.dirname(os.__file__), 'socket.py')
    imp.load_source('socket', psocket)

回答by Tomisin Abiodun

Jan Dolejsi,

扬·多莱西

If you're using GAE's Sockets, you can get SSL support without any hacks by simply loading the SSL library.

Simply add this to your app.yaml file:

libraries: - name: ssl
- version: latest

如果您使用 GAE 的 Sockets,您可以通过简单地加载 SSL 库来获得 SSL 支持而无需任何黑客攻击。

只需将其添加到您的 app.yaml 文件中:

库:-名称:ssl-
版本:最新

If you're experiencing RAND_egd error, just change "-version: latest" in your app.yaml, to "-version: 2.7"!

如果您遇到 RAND_egd 错误,只需将 app.yaml 中的“-version: latest”更改为“-version: 2.7”即可!