前端-requests-flask的对应关系
flask获得请求参数`request.args
key1=request.args["key1"]
key2=request.args["key2"]
requests发送get请求,params
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get("http://127.0.0.1:5000/get", params=payload)
前端
1)使用
http://127.0.0.1:5000/?key1=value1&key2=value2
2)或使用form的get方法提交表单
<form action="/" method="get">
<p>First name: <input type="text" name="key1" /></p>
<p>Last name: <input type="text" name="key2" /></p>
<input type="submit" value="Submit" />
</form>
代码:
flask
from flask import Flask,request,jsonify
app = Flask("__main__")
@app.route('/')
def index():
value1=request.args["key1"]
value2=request.args["key2"]
return jsonify({"key1":value1,"key2":value2})
if __name__ == '__main__':
app.run(host="localhost",port=5000,debug=True)
requests
import requests
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get("http://127.0.0.1:5000/", params=payload)
r.text
前端
1)url连接框内输入
http://127.0.0.1:5000/?key1=value1&key2=value2
- 表单形式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="/" method="get">
<p>First name: <input type="text" name="key1" /></p>
<p>Last name: <input type="text" name="key2" /></p>
<input type="submit" value="Submit" />
</form>
</body>
</html>