Flask无法打印到控制台 [英] Flask doesn't print to console

查看:1472
本文介绍了Flask无法打印到控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to flask, and I'm trying to add print info to debug server side code. When launch my flask app with debug=True, i can't get any info print to console

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

I tried to use logging instead, but no success. So how to debug flask program with console.

@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代码,并且控制台显示以下信息

I fixed server side 500 error issue, now request get 200 code, and console displays following info

$ 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 -

仍然没有打印命令中的信息

Still no info from print command

推荐答案

尝试一下,看看是否有帮助:

Try this and see if it helps:

对于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,您不需要从未来 print_function导入:

For python3 you don't need to import from future print_function:

import sys

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

查看是否有助于打印到控制台.

See if it helps to print to console.

这篇关于Flask无法打印到控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆