由于 sys arg 参数,Flask 应用程序忽略了 cURL 请求的代码,但在 sys arg 中未传递任何内容

Flask app ignores code for a cURL request due to a sys arg param but nothing is passed in sys arg

我有一个带有以下代码的小型烧瓶应用程序。 请注意,我已经删除了大量代码来简化我的问题。如果需要重新添加或缺少任何内容,请告诉我。

1   @app.route("/app/data/", methods=['POST'])
2   def methodpost():
3
4       if 'Content-Type' in request.headers and request.headers['Content-Type'] == 'application/json':
5
6       #some_code
7       #some_more_code
8
9       if not any(['unittest' or 'discover' in arg for arg in sys.argv]):
10
11          #code_calls_external_stuff
12          #more_code
13
14
15      return msg
16
17
18      app.run(host=0.0.0.0, debug=debug, port=5000)

当我使用以下命令对其执行 cURL 时:

curl -X POST "localhost:5000/app/data/" -H "Content-Type: application/json" -d @jsonBody

它不会进入 LINE-11 和 LINE-12。

但是,当我将 LINE-9 修改为以下内容时:

----FROM:----
if not any(['unittest' or 'discover' in arg for arg in sys.argv]):
----TO:----
if not any(['unittest' in arg for arg in sys.argv]):

然后它工作并调用 LINE-11 和 LINE-12。上面只是添加了所以我可以 运行 UnitTest 和 Tox 而应用程序不是 运行ning 因为我不想打外部电话......有没有办法在保持代码原样。

if not any(['unittest' or 'discover' in arg for arg in sys.argv]):

没有按照您的意愿行事。将其简化为

['x' or 'y' in a for a in ['y']]

运行 它在 REPL 中,并考虑为什么它以 ['x'].

响应