我试图将安全上下文添加到一个简单的 flask 应用程序中
import flask
import ssl
app = flask.Flask(__name__)
@app.route('/', methods=['GET'])
def home():
return "<h1>THIS IS HOME</h1>"
ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
ctx.load_cert_chain('crt-key-crs/download-system.crt', 'crt-key-crs/download-system.key')
if __name__ == '__main__':
app.run(debug=True, use_reloader=False, ssl_context=ctx)
并且在输入密码短语后运行它时发生以下错误:
Traceback (most recent call last):
File "flask-test.py", line 16, in <module>
app.run(debug=True, use_reloader=False, ssl_context=ctx)
File "/usr/lib/python3/dist-packages/flask/app.py", line 772, in run
run_simple(host, port, self, **options)
File "/usr/lib/python3/dist-packages/werkzeug/serving.py", line 710, in run_simple
inner()
File "/usr/lib/python3/dist-packages/werkzeug/serving.py", line 692, in inner
passthrough_errors, ssl_context).serve_forever()
File "/usr/lib/python3/dist-packages/werkzeug/serving.py", line 486, in make_server
passthrough_errors, ssl_context)
File "/usr/lib/python3/dist-packages/werkzeug/serving.py", line 425, in __init__
self.socket = tsafe.Connection(ssl_context, self.socket)
File "/usr/lib/python3/dist-packages/OpenSSL/tsafe.py", line 11, in __init__
self._ssl_conn = apply(_ssl.Connection, args)
NameError: name 'apply' is not defined
然后我在文档中发现 apply(function, *args, **kwargs) 被替换为 function(*args, **kwargs) 所以我去了文件 /usr/lib/python3/dist -packages/OpenSSL/tsafe.py
并手动修改该行,再次运行时出现以下错误:
Traceback (most recent call last):
File "flask-test.py", line 16, in <module>
app.run(debug=True, use_reloader=False, ssl_context=ctx)
File "/usr/lib/python3/dist-packages/flask/app.py", line 772, in run
run_simple(host, port, self, **options)
File "/usr/lib/python3/dist-packages/werkzeug/serving.py", line 710, in run_simple
inner()
File "/usr/lib/python3/dist-packages/werkzeug/serving.py", line 692, in inner
passthrough_errors, ssl_context).serve_forever()
File "/usr/lib/python3/dist-packages/werkzeug/serving.py", line 486, in make_server
passthrough_errors, ssl_context)
File "/usr/lib/python3/dist-packages/werkzeug/serving.py", line 425, in __init__
self.socket = tsafe.Connection(ssl_context, self.socket)
File "/usr/lib/python3/dist-packages/OpenSSL/tsafe.py", line 11, in __init__
self._ssl_conn = _ssl.Connection(*args)
File "/usr/lib/python3/dist-packages/OpenSSL/SSL.py", line 804, in __init__
raise TypeError("context must be a Context instance")
TypeError: context must be a Context instance
在此之后,我知道 apply 已从 python3.x 中完全删除,我使用 python2.7.9 运行我的应用程序,以检查我在修改该行时是否做错了什么,显然我没有,因为同样的错误再次发生。那么,为了在我的 Flask 应用程序中使用安全上下文,我需要做什么或更改我的代码?提前致谢。
我是一名优秀的程序员,十分优秀!