AttributeError:'上下文'对象没有属性' wrap_socket'

时间:2015-02-18 08:35:41

标签: python flask openssl werkzeug pyopenssl

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

非常感谢您提前!我很乐意提供任何帮助

1 个答案:

答案 0 :(得分:67)

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

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/