NOTE
1.初始化:所有的Flask程序都必须创建一个程序实例.
app = Flask(__name__) # 向Flask的构造函数传入参数__name__
2.路由和视图函数(VF):
Clients --(requests)--> Server --> Flask Object --(mapping/route)--> View Function --(responses)--> Server --> Clients
# 通过Object提供的app.route修饰器将函数index()注册为View Function
@app.route('/') # 路由:URL
def index() :
return '<h1>HelloWorld!<h1>'
3.启动服务器:使用run方法。
if __name__ == '__main__':
app.run(debug = True) # debug模式
4.执行
Hello.py
#!/usr/bin/env python
# import module
from flask import Flask
# Creat an Object
app = Flask(__name__)
# URL Route
@app.route('/')
def index() :
return '<h1>HelloWorld!<h1>'
if __name__ == '__main__':
app.run(debug = True)
(venv) sh-3.2# python hello.py
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger pin code: 195-717-834
127.0.0.1 - - [15/Feb/2017 22:43:35] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [15/Feb/2017 22:43:36] "GET /favicon.ico HTTP/1.1" 404 -
效果:
2017/2/16