于POST和GET
①get一般用于向服务器请求获取数据,请求参数存放在URL中,并在地址栏可见,而post是向服务器提交数据,数据放置在容器(HTML HEADER)内且不可见;
②将一个method定义成RequestMethod.GET时,可以直接通过地址访问,这非常方便我们在开发的时候调用到我们的接口并进行测试;
③同样的接口,将其method更改为RequestMethod.POST时,你会发现接口在地址栏访问不了了,只有向服务器发起一个POST请求时才起作用
import sys request:<Request 'http://127.0.0.1:80/params/?name=coco&pwd=123456' [GET]>
from flask import Flask, request
from flask_script import Manager
# 生成Flask对象,默认一些配置,包括静态文件,模板文件的配置
app = Flask(__name__) app:<Flask 'flask1'>
@app.route('/')
def hello():
# 127.0.0.2:5000/
return 'Hello World'
@app.route('/hello/')
def hello_girl():
# 127.0.0.2:5000/hello/
return 'hello girl '
@app.route('/day01/<int:id>/')
def hello_day(id):
# 127.0.0.2:5000/hello/
return 'hello day01 %s' % id
@app.route('/name/<string:name>')
def hello_name(name):
return 'hello name: %s' % name
@app.route('/sname/<name>')
def hello_sname(name):
return 'hello sname:%s' % name
@app.route('/params/')
def params():
request
# # 获取get请求传递的参数
name = request.args['name'] name:'coco'
# # request.args.get('name')
# pwd = request.args['pwd']
return '获get取参数'
# ':name=%s pwd=%s' % (name,pwd)
@app.route('/post_params/', methods=['POST'])
def post_params():
name = request.form['name']
age = request.get.form['age',18]
return '获取post参数'
manage = Manager(app)
if __name__ == '__main__':
# 启动web服务器;
# print(sys.argv) # 0.0.0.0 80222
# host = sys.argv[1]
# port = sys.argv[2]
# Terminal: python flask1.py 0.0.0.0 80
# app.run(host=host, port=port, debug=True)
# pip install -r requirement.txt
# 批量安装文件中的包
manage.run()