美文网首页
python flask高级编程(1)

python flask高级编程(1)

作者: 宁静消失何如 | 来源:发表于2019-06-24 20:19 被阅读0次

最简易的flask

from flask import Flask
app = Flask(__name__)
# 装饰器路由
@app.route('/hello')
def hello():
    # 基于类的视图()
    return 'Hello, QiYue'

app.run(debug=True)

将路由与类分离

from flask import Flask
app = Flask(__name__)


# @app.route('/hello')
def hello():
    # 基于累的视图()
    return 'Hello, QiYue'


app.add_url_rule('/hello', view_func=hello)


app.run(debug=True)

正经的是这样

from flask import Flask, make_response
app = Flask(__name__)
app.config.from_object('config')



# @app.route('/hello')
def hello():
    # 基于类的视图()
    # status code 200, 404, 301
    # content-type http headers  告诉浏览器如何解析
    # content-type = text/html
    headers = {
        'content-type': 'text/plain',
        'content-type': 'application/json',
        'location': 'http://www.bing.com'
    }
    return ('<html></html>', 301, headers)
    # return 'Hello, QiYue'

def helloo():
    return  'Hello, QiYue'


app.add_url_rule('/hello', view_func=hello)


if __name__ == '__main__':
    # 生产环境 nginx + uwsgi
    app.run(debug=app.config['DEBUG'], host='0.0.0.0', port=80)

相关文章

网友评论

      本文标题:python flask高级编程(1)

      本文链接:https://www.haomeiwen.com/subject/bnxaqctx.html