欢迎关注我👆,收藏下次不迷路┗|`O′|┛ 嗷~~
目录
一.模块介绍
二.数据处理演示
2.1客户端
2.2服务端
三.完整代码
一.模块介绍
数据处理模块主要由flask框架实现,其他模块通过post请求与数据处理模块进行交互,实现信息传输。
二.数据处理演示
2.1客户端
import requests
import json
# 目标URL
url = 'http://127.0.0.1:6999/receive_json'
# 要发送的JSON数据
data = {
'key1': 'value1',
'key2': 'value2'
}
# 发送POST请求
response = requests.post(url, json=data)
# 打印响应内容
print(response.text)
# 如果你想要以JSON格式处理响应内容
response_data = response.json()
print(response_data)
print(json.dumps(response_data, indent=4)) # 美化打印
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
2.2服务端
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/receive_json', methods=['POST'])
def receive_json():
# 获取 JSON 数据
json_data = request.get_json()
# 假设我们只是简单地返回接收到的 JSON 数据
return jsonify(json_data), 200
if __name__ == '__main__':
app.run(host='127.0.0.1', port=6999, debug=True)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
三.完整代码
from flask import Flask, request, jsonify
import os
app = Flask(__name__)
UPLOAD_FOLDER = '../temp_files/face_pics'
UPLOAD_FACE_LOCK=False
MASTER_LOCK=False
FACES_UPLOADED=[]
SOUND_WORDS=''
FACE_POSE=["NONE","NONE"]
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/upload_face_pics_file', methods=['POST'])
def upload_face_pics_file():
if 'file' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
if file:
filename = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
file.save(filename)
return jsonify({'message': 'File uploaded successfully', 'filename': file.filename}), 200
@app.route('/upload_face_path',methods=['POST'])
def upload_face_path():
global FACES_UPLOADED
# 获取 JSON 数据
json_data = request.get_json()
FACES_UPLOADED.append(json_data['path_data'])
return jsonify({'upload_status': 'OK'}), 200
@app.route('/download_face_path',methods=['POST'])
def download_face_path():
global FACES_UPLOADED
if len(FACES_UPLOADED)>0:
return jsonify({'download_status':"HAVEDATA",'download_data': FACES_UPLOADED.pop(0)}), 200
else:
return jsonify({'download_status':"NODATA"}), 200
@app.route('/set_upload_face_check',methods=['POST'])
def set_upload_face_check():
global UPLOAD_FACE_LOCK
UPLOAD_FACE_LOCK=True
return jsonify({'set_status': 'OK'}), 200
@app.route('/upload_face_check',methods=['POST'])
def upload_face_check():
global UPLOAD_FACE_LOCK
if UPLOAD_FACE_LOCK:
UPLOAD_FACE_LOCK=False
return jsonify({'check': 'YES'}), 200
else:
return jsonify({'check': 'NO'}), 200
@app.route('/set_face_pose',methods=['POST'])
def set_face_pose():
global FACE_POSE
FACE_POSE=[request.get_json()["loc_x"],request.get_json()["loc_y"]]
return jsonify({'status': "OK"}), 200
@app.route('/get_face_pose',methods=['POST'])
def get_face_pose():
global FACE_POSE
loc_x=FACE_POSE[0]
loc_y=FACE_POSE[1]
return jsonify({'loc_x': loc_x,'loc_y': loc_y}), 200
@app.route('/get_sound_state',methods=['POST'])
def get_sound_state():
global SOUND_WORDS
ret_word=SOUND_WORDS
SOUND_WORDS=''
return jsonify({'words': ret_word}), 200
@app.route('/set_sound_state',methods=['POST'])
def set_sound_state():
global SOUND_WORDS
SOUND_WORDS=request.get_json()["words"]
return jsonify({'status': "OK"}), 200
@app.route('/set_master_status_YES',methods=['POST'])
def set_master_status_YES():
global MASTER_LOCK
MASTER_LOCK=True
return jsonify({'status': str(MASTER_LOCK)}), 200
@app.route('/set_master_status_NO',methods=['POST'])
def set_master_status_NO():
global MASTER_LOCK
MASTER_LOCK=False
return jsonify({'status': str(MASTER_LOCK)}), 200
@app.route('/get_master_status',methods=['POST'])
def get_master_status():
global MASTER_LOCK
if MASTER_LOCK:
return jsonify({'status': 'True'}), 200
else:
return jsonify({'status': 'False'}), 200
#@app.route('/receive_json', methods=['POST'])
#def receive_json():
# # 获取 JSON 数据
# json_data = request.get_json()
# # 假设我们只是简单地返回接收到的 JSON 数据
# return jsonify(json_data), 200
if __name__ == '__main__':
if os.path.exists(UPLOAD_FOLDER):
os.system('rm -rf ../temp_files/')
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
app.run(host='127.0.0.1', port=6999, debug=True)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
非常感谢您花时间阅读我的博客,希望这些分享能为您带来启发和帮助。期待您的反馈与交流,让我们共同成长,再次感谢!
👇个人网站👇