AttributeError: 'Context' object has no attribute 'wrap_socket'

AttributeError: 'Context' object has no attribute 'wrap_socket'

我正在尝试设置一个使用 OpenSSL 上下文的 Flask 服务器。 但是,由于我将脚本移动到不同的服务器上,它会不断抛出以下错误,无论我使用的是 Python 2.7 还是 3.4,也无论我选择了哪种 SSL 方法(SSLv23/TLSv1/...) :

  File "/usr/lib/python3.4/threading.py", line 920, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.4/threading.py", line 868, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py", line 602, in inner
    passthrough_errors, ssl_context).serve_forever()
  File "/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py", line 506, in make_server
    passthrough_errors, ssl_context)
  File "/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py", line 450, in __init__
    self.socket = ssl_context.wrap_socket(self.socket,
AttributeError: 'Context' object has no attribute 'wrap_socket'

对应代码如下:

if __name__ == "__main__":
        context = SSL.Context(SSL.SSLv23_METHOD)
        context.use_privatekey_file('key.key')
        context.use_certificate_file('cert.crt')
        app.run(host='0.0.0.0', port=80, ssl_context=context, threaded=True, debug=True)

非常感谢您!我很高兴得到任何帮助

从 0.10 开始,Werkzeug 不再支持 OpenSSL 上下文。做出此决定是因为 ssl.SSLContext 跨 Python 版本更容易支持。您可以选择重写此代码:

if __name__ == "__main__":
    context = ('cert.crt', 'key.key')
    app.run(host='0.0.0.0', port=80, ssl_context=context, threaded=True, debug=True)

有关所有可能性,请参阅 http://werkzeug.pocoo.org/docs/latest/serving/