From cdada0e3047a3451f42547e48208fe49e936cfcb Mon Sep 17 00:00:00 2001 From: wangzhiming <1115712903@qq.com> Date: Tue, 6 May 2025 17:57:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 server.py diff --git a/server.py b/server.py new file mode 100644 index 0000000..98c5213 --- /dev/null +++ b/server.py @@ -0,0 +1,63 @@ +from flask import Flask, request, jsonify, render_template +import subprocess +import sys +from datetime import datetime +import os +import json +app = Flask(__name__) + +@app.route('/') +def home(): + return render_template('index.html') + +@app.route('/calculate', methods=['POST']) +def calculate(): + date_str = request.form.get('date') + time_str = request.form.get('time', '00:00:00') # 默认为00:00:00 + + try: + # 验证日期和时间格式 + datetime.strptime(date_str, '%Y-%m-%d') # 先单独验证日期 + datetime.strptime(time_str, '%H:%M:%S') # 再验证时间 + + # 组合日期和时间 + datetime_str = f"{date_str} {time_str}" + + api_path = os.path.join(os.path.dirname(__file__), 'API.py') + result = subprocess.run( + [sys.executable, api_path, datetime_str], + capture_output=True, + text=True, # 确保输出是文本 + check=True + ) + + # 打印完整输出用于调试 + print("=== API.py 原始输出 ===") + print("stdout:", result.stdout) + print("stderr:", result.stderr) + + try: + json_output = json.loads(result.stdout) + return jsonify({'success': True, 'result': json_output}) + except json.JSONDecodeError as e: + print("JSON 解析失败:", e) + print("原始输出:", result.stdout) + return jsonify({ + 'success': False, + 'error': f'JSON解析失败: {str(e)}', + 'raw_output': result.stdout # 返回原始输出用于调试 + }) + + except ValueError: + return jsonify({'success': False, 'error': '无效的日期格式'}) + except subprocess.CalledProcessError as e: + return jsonify({ + 'success': False, + 'error': f'计算失败: {e.stderr}', + 'stdout': e.stdout + }) + except Exception as e: + return jsonify({'success': False, 'error': str(e)}) + +if __name__ == '__main__': + app.run(debug=True,host='0.0.0.0',port=5000) \ No newline at end of file