gpt4 book ai didi

python-3.x - 如何向 flask 添加安全上下文?

转载 作者:太空宇宙 更新时间:2023-11-03 12:48:48 26 4
gpt4 key购买 nike

我试图将安全上下文添加到一个简单的 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 应用程序中使用安全上下文,我需要做什么或更改我的代码?提前致谢。

最佳答案

经过大量搜索和测试,我发现代码没有问题,问题是Werkzeug服务器的版本。我使用 sudo pip3 install Werkzeug --upgrade 进行了升级,现在一切正常。

关于python-3.x - 如何向 flask 添加安全上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30190182/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com