gpt4 book ai didi

python - flask 不打印到控制台

转载 作者:IT老高 更新时间:2023-10-28 20:44:05 53 4
gpt4 key购买 nike

我是 flask 的新手,我正在尝试添加打印信息来调试服务器端代码。当使用 debug=True 启动我的 flask 应用程序时,我无法将任何信息打印到控制台

我尝试改用日志记录,但没有成功。那么如何用控制台调试flask程序呢。

@app.route('/getJSONResult', methods=['GET', 'POST'])
def getJSONResult():

if request.method == 'POST':
uut = request.form['uut']
notes = request.form['notes']
temperature = request.form['temperature']

logging.info("enter getJSONReuslt")
print('enter getJSONReuslt')
filter_by_query = {k: v for k, v in {
'uut': uut, 'notes': notes, 'temperature': temperature}.items() if v != ""}
s = session.query(UUT_TEST_INFO).filter_by(**filter_by_query).first()
return jsonify(s.serialize)

if __name__ == '__main__':
app.secret_key = ''.join(random.choice(
string.ascii_uppercase + string.digits) for x in range(32))
app.debug = True
app.run(host='127.0.0.1', port=5000)


> 127.0.0.1 - - [07/Jun/2017 15:20:48] "GET /qyer HTTP/1.1" 200 -
> 127.0.0.1 - - [07/Jun/2017 15:20:48] "GET /static/css/bootstrap.min.css HTTP/1.1" 200 -
> 127.0.0.1 - - [07/Jun/2017 15:20:48] "GET /static/js/bootstrap.min.js HTTP/1.1" 200 -
> 127.0.0.1 - - [07/Jun/2017 15:20:51] "GET /static/css/bootstrap.min.css.map HTTP/1.1" 200 -
> 127.0.0.1 - - [07/Jun/2017 15:21:58] "POST /getJSONResult HTTP/1.1" 500 -

我修复了服务器端 500 错误问题,现在请求获取 200 代码,控制台显示以下信息

$ python project.py
INFO:werkzeug: * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
INFO:werkzeug: * Restarting with stat
WARNING:werkzeug: * Debugger is active!
INFO:werkzeug: * Debugger pin code: 158-624-607
INFO:werkzeug:127.0.0.1 - - [08/Jun/2017 11:33:33] "GET /qyer HTTP/1.1" 200 -
INFO:root:Enter getJSONResult
INFO:werkzeug:127.0.0.1 - - [08/Jun/2017 11:33:43] "POST /getJSONResult HTTP/1.1" 200 -

仍然没有来自打印命令的信息

最佳答案

试试这个,看看是否有帮助:

对于python2:

from __future__ import print_function
import sys

print('This is error output', file=sys.stderr)
print('This is standard output', file=sys.stdout)

对于python3,您不需要从future print_function 导入:

import sys

print('This is error output', file=sys.stderr)
print('This is standard output', file=sys.stdout)

看看它是否有助于打印到控制台。

关于python - flask 不打印到控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44405708/

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