赞
踩
一、接收http请求与返回响应
在Flask中,可以通过@app.route装饰器来定义路由函数。
@app.route('/BringGoods',methods = ['POST', 'GET'])
GET请求:使用request.args.get('key')或者request.values.get('key')来获取URL中的参数。
POST请求:
实例代码:
- @app.route('/BringGoods',methods = ['POST', 'GET'])
- def bringgoods():
- if request.method == 'POST':
- ord = request.json.get("ord")
- print(ord)
- data = {
- "msg":"ok"
- }
- return json.dumps(data)
- if request.method == 'GET':
- ord = request.values.get("name")
- print(ord)
- data = {
- "msg":"ok"
- }
- return json.dumps(data)

另附:postman接口测试图
POST方式:
GET方式:
二、 发送http请求
- import requests
- import datetime
- import json
- #接口调用post传参
- def send_data(url,data):
- headers =
- {
- "Content-Type":"application/json"
- }
- try:
- response = requests.post(url=url,json=data,headers=headers)
- response.raise_for_status()
- processed_data = json.loads(response.content)
- print("processed_data",processed_data)
- except requests.exceptions.RequestException as e:
- print("error",e)
-
-
- if __name__ == "__main__":
- wms_url = "http://*.*.*.*:7000/BringGoods"
- now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
- nopark_data ={
- "time":now_time,
- "ord": "44"
- }
- print(nopark_data)
- send_data(wms_url,nopark_data)
-

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。