python-tutorial icon indicating copy to clipboard operation
python-tutorial copied to clipboard

python中的flask库

Open Wscats opened this issue 7 years ago • 0 comments

安装

D:\Python27\Scripts,不同环境路径可能不一样,全局安装

pip install Flask

image

启动

新建一个名为server.py文件,注意不要用flask命名文件,因为会冲突,并输入以下代码

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()

在该文件所在的位置打开命令行,并执行以下命令

python server.py

image

在浏览器中输入以下dizh地址,我们将会看到浏览器页面输出Hello World!

http://localhost:5000/

路由

可以用@app.route('/xxx')定义多个路由,也可以用@app.route('/user/<xxxx>')形式来传递get请求的参数

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

@app.route('/hello')
def hello():
    return 'wscats'

@app.route('/user/<username>')
def show_user_profile(username):
    # show the user profile for that user
    return 'User %s' % username

if __name__ == '__main__':
    app.run()

Wscats avatar Mar 05 '18 08:03 Wscats