最简易的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)