30

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

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

4

1 回答 1

74

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

于 2015-02-18T17:44:14.550 回答