无修改
This commit is contained in:
parent
f5ee3ed361
commit
0d080a38ff
BIN
25c859cf-e15d-458d-82e9-81039c49f9ed.png
Executable file
BIN
25c859cf-e15d-458d-82e9-81039c49f9ed.png
Executable file
Binary file not shown.
|
After Width: | Height: | Size: 36 KiB |
BIN
7406b499-a8cb-4b9f-ad15-45558dd1602b.png
Executable file
BIN
7406b499-a8cb-4b9f-ad15-45558dd1602b.png
Executable file
Binary file not shown.
|
After Width: | Height: | Size: 18 KiB |
39
all_server_open(云).py
Executable file
39
all_server_open(云).py
Executable file
@ -0,0 +1,39 @@
|
||||
from flask import Flask, jsonify
|
||||
from flask_cors import CORS
|
||||
import json
|
||||
from decimal import Decimal
|
||||
from datetime import datetime, date
|
||||
|
||||
# 导入蓝图
|
||||
from example_info_add import example_info_add_bp
|
||||
from user_add import user_add_bp
|
||||
|
||||
|
||||
# 极简JSON编码器(处理特殊数据类型)
|
||||
class CustomJSONEncoder(json.JSONEncoder):
|
||||
def default(self, obj):
|
||||
if isinstance(obj, Decimal):
|
||||
return float(obj)
|
||||
elif isinstance(obj, (datetime, date)):
|
||||
return obj.strftime('%Y-%m-%d %H:%M:%S')
|
||||
return super().default(obj)
|
||||
|
||||
# 创建单个Flask应用
|
||||
app = Flask(__name__)
|
||||
CORS(app) # 允许跨域
|
||||
app.json_encoder = CustomJSONEncoder
|
||||
app.config['JSON_SORT_KEYS'] = False
|
||||
|
||||
|
||||
# 注册所有蓝图
|
||||
app.register_blueprint(example_info_add_bp)
|
||||
app.register_blueprint(user_add_bp)
|
||||
|
||||
if __name__ == '__main__':
|
||||
# 配置服务(直接修改端口)
|
||||
host = '0.0.0.0'
|
||||
port = 5000
|
||||
print(f"服务启动:http://{host}:{port}")
|
||||
print("按 Ctrl+C 停止服务...")
|
||||
app.run(host=host, port=port, debug=False) # debug=False 生产环境推荐
|
||||
|
||||
56
db_con.py
Executable file
56
db_con.py
Executable file
@ -0,0 +1,56 @@
|
||||
import pymysql
|
||||
|
||||
# 数据库配置
|
||||
db_config = {
|
||||
'host': "47.106.222.181",
|
||||
'user': "group3",
|
||||
'password': 'group3',
|
||||
'database': 'ClassDB',
|
||||
'port':25083
|
||||
}
|
||||
|
||||
def con_mysql(sql_code, params=None):
|
||||
conn = None
|
||||
try:
|
||||
conn = pymysql.connect(** db_config)
|
||||
conn.ping(reconnect=True)
|
||||
cursor = conn.cursor()
|
||||
print(f"执行SQL: {sql_code}")
|
||||
print(f"参数: {params}")
|
||||
|
||||
# 执行SQL
|
||||
if params:
|
||||
cursor.execute(sql_code, params)
|
||||
else:
|
||||
cursor.execute(sql_code)
|
||||
|
||||
# 增/删/改操作
|
||||
if sql_code.strip().upper().startswith(('INSERT', 'UPDATE', 'DELETE')):
|
||||
conn.commit()
|
||||
result = {
|
||||
'success': True,
|
||||
'affected_rows': cursor.rowcount
|
||||
}
|
||||
# 对INSERT操作额外返回自增ID
|
||||
if sql_code.strip().upper().startswith('INSERT'):
|
||||
result['lastrowid'] = cursor.lastrowid
|
||||
return result
|
||||
|
||||
# 查询操作
|
||||
elif sql_code.strip().upper().startswith('SELECT'):
|
||||
data = cursor.fetchall()
|
||||
columns = [col[0] for col in cursor.description]
|
||||
return {
|
||||
'success': True,
|
||||
'data': [dict(zip(columns, row)) for row in data]
|
||||
}
|
||||
|
||||
return {'success': True}
|
||||
|
||||
except pymysql.MySQLError as err:
|
||||
if conn:
|
||||
conn.rollback()
|
||||
return {'success': False, 'error': f"{type(err).__name__}: {err}"}
|
||||
finally:
|
||||
if conn:
|
||||
conn.close()
|
||||
73
example_info_add.py
Executable file
73
example_info_add.py
Executable file
@ -0,0 +1,73 @@
|
||||
from flask import Blueprint, request, jsonify
|
||||
from db_con import con_mysql
|
||||
|
||||
example_info_add_bp = Blueprint('example_info_add', __name__)
|
||||
|
||||
"""
|
||||
API接口:添加example_info数据
|
||||
接口路径:POST /api/example_info/add
|
||||
|
||||
请求体示例:
|
||||
{
|
||||
"emotion_state": "开心",
|
||||
"video_path": "/videos/test1.mp4",
|
||||
"confidence": 0.95
|
||||
}
|
||||
|
||||
字段说明:
|
||||
- emotion_state (可选):字符串,描述emotion_state
|
||||
- video_path (可选):字符串,视频文件路径
|
||||
- confidence (可选):浮点数,confidence值(0-1)
|
||||
"""
|
||||
|
||||
@example_info_add_bp.route('/api/example_info/add', methods=['POST'])
|
||||
def add_example_info():
|
||||
try:
|
||||
data = request.get_json()
|
||||
|
||||
# 验证必需字段
|
||||
if not data:
|
||||
return jsonify({
|
||||
"success": False,
|
||||
"message": "请求数据不能为空"
|
||||
}), 400
|
||||
|
||||
# 获取请求数据
|
||||
emotion_state = data.get('emotion_state')
|
||||
video_path = data.get('video_path')
|
||||
confidence = data.get('confidence')
|
||||
|
||||
# 构建SQL语句
|
||||
sql = """
|
||||
INSERT INTO group3_emo_info (emotion_state, video_path, confidence)
|
||||
VALUES (%s, %s, %s)
|
||||
"""
|
||||
params = (emotion_state, video_path, confidence)
|
||||
|
||||
# 执行SQL
|
||||
result = con_mysql(sql, params)
|
||||
|
||||
if result['success']:
|
||||
return jsonify({
|
||||
"success": True,
|
||||
"message": "example_info数据添加成功",
|
||||
"data": {
|
||||
"id": result.get('lastrowid'),
|
||||
"emotion_state": emotion_state,
|
||||
"video_path": video_path,
|
||||
"confidence": confidence
|
||||
}
|
||||
}), 201
|
||||
else:
|
||||
return jsonify({
|
||||
"success": False,
|
||||
"message": "添加失败",
|
||||
"error": result.get('error', '未知错误')
|
||||
}), 500
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({
|
||||
"success": False,
|
||||
"message": "服务器内部错误",
|
||||
"error": str(e)
|
||||
}), 500
|
||||
82
user_add.py
Executable file
82
user_add.py
Executable file
@ -0,0 +1,82 @@
|
||||
from flask import Blueprint, request, jsonify
|
||||
from db_con import con_mysql
|
||||
|
||||
user_add_bp = Blueprint('user_add', __name__)
|
||||
|
||||
"""
|
||||
API接口:添加用户
|
||||
接口路径:POST /api/user/add
|
||||
|
||||
请求体示例:
|
||||
{
|
||||
"username": "testuser",
|
||||
"password": "12345678"
|
||||
}
|
||||
|
||||
字段说明:
|
||||
- username (必需):字符串,用户名(最长8个字符)
|
||||
- password (可选):字符串,密码(最长8个字符)
|
||||
"""
|
||||
|
||||
@user_add_bp.route('/api/user/add', methods=['POST'])
|
||||
def add_user():
|
||||
try:
|
||||
data = request.get_json()
|
||||
|
||||
# 验证必需字段
|
||||
if not data:
|
||||
return jsonify({
|
||||
"success": False,
|
||||
"message": "请求数据不能为空"
|
||||
}), 400
|
||||
|
||||
username = data.get('username')
|
||||
password = data.get('password')
|
||||
|
||||
# 验证用户名是否为空
|
||||
if not username:
|
||||
return jsonify({
|
||||
"success": False,
|
||||
"message": "用户名不能为空"
|
||||
}), 400
|
||||
|
||||
# 验证用户名长度
|
||||
if len(username) > 8:
|
||||
return jsonify({
|
||||
"success": False,
|
||||
"message": "用户名长度不能超过8个字符"
|
||||
}), 400
|
||||
|
||||
# 构建SQL语句
|
||||
sql = """
|
||||
INSERT INTO group3_user (username, password)
|
||||
VALUES (%s, %s)
|
||||
"""
|
||||
params = (username, password)
|
||||
|
||||
# 执行SQL
|
||||
result = con_mysql(sql, params)
|
||||
|
||||
if result['success']:
|
||||
return jsonify({
|
||||
"success": True,
|
||||
"message": "用户添加成功",
|
||||
"data": {
|
||||
"username": username,
|
||||
"password": password,
|
||||
"affected_rows": result.get('affected_rows')
|
||||
}
|
||||
}), 201
|
||||
else:
|
||||
return jsonify({
|
||||
"success": False,
|
||||
"message": "添加用户失败",
|
||||
"error": result.get('error', '未知错误')
|
||||
}), 500
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({
|
||||
"success": False,
|
||||
"message": "服务器内部错误",
|
||||
"error": str(e)
|
||||
}), 500
|
||||
Loading…
x
Reference in New Issue
Block a user