Python AttributeError: 'Context' 对象没有属性 'wrap_socket'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28579142/
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 03:28:47  来源:igfitidea点击:

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

pythonflaskopensslwerkzeugpyopenssl

提问by Nils Milchert

I am trying to set up a Flask server that uses an OpenSSL context. However, since I moved the script on a different server, it keeps throwing the following error, no matter if I am using Python 2.7 or 3.4 and no matter which SSL method I chose (SSLv23 / TLSv1/...):

我正在尝试设置一个使用 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'

The according code below:

下面的相应代码:

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)

Thank you very much in advance! I am happy for any help

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

采纳答案by Markus Unterwaditzer

As of 0.10, Werkzeug doesn't support OpenSSL contexts anymore. This decision was made because it is easier to support ssl.SSLContextacross Python versions. Your option to re-write this code is this one:

从 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)

See http://werkzeug.pocoo.org/docs/latest/serving/for all possibilities.

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