发送post请求:
import requests
# 定义请求的 URL
url = 'http://192.168.0.100:5050/api'
# 定义请求的参数
data = {
'file': 'file.jpg',
}
headers = {'Content-Type': 'application/json'}
# 发送 POST 请求
response = requests.post(url, json=data, headers=headers)
# 处理响应
if response.status_code == 200:
# 请求成功
result = response.json()
print(result)
else:
# 请求失败
print('请求失败:', response.status_code, response.text)
restful api:
from flask import Flask,request,jsonify
app = Flask(__name__)
@app.route("/api", methods=['POST'])
def get_info():
try:
file_name = request.get_json()["file"]
#逻辑
#...
ret = {"data": data}
return jsonify(ret)
except BaseException as err:
...
ret = {"data": str(err}
return jsonify(ret)
if __name__ == '__main__':
app.run(debug=True,host="0.0.0.0", port=5050)