from flask import Flask, request, render_template_string import os import datetime app = Flask(__name__) FILE_PATH = "1.txt" HTML = ''' 账号密码提交

填写账号密码

账号:

密码:

{{ message }}

''' def is_valid_date(): today = datetime.datetime.now() return 1 <= today.day <= 28 @app.route('/', methods=['GET', 'POST']) def index(): message = "" if request.method == 'POST': if not is_valid_date(): message = "不在有效期,无法确认" else: username = request.form.get("username", "").strip() password = request.form.get("password", "").strip() if username and password: update_txt_file(username, password) message = "提交成功!" else: message = "请填写账号和密码" return render_template_string(HTML, message=message) def update_txt_file(username, password): if not os.path.exists(FILE_PATH): with open(FILE_PATH, 'w') as f: f.write(f"{username}\n{password}\n") return with open(FILE_PATH, 'r') as f: lines = f.readlines() new_lines = [] found = False i = 0 while i < len(lines): u = lines[i].strip() p = lines[i+1].strip() if i+1 < len(lines) else "" if u == username: new_lines.extend([username + '\n', password + '\n']) found = True i += 2 else: new_lines.extend([u + '\n', p + '\n']) i += 2 if not found: new_lines.extend([username + '\n', password + '\n']) with open(FILE_PATH, 'w') as f: f.writelines(new_lines) from flask import send_file @app.route('/download', methods=['GET']) def download(): if os.path.exists(FILE_PATH): return send_file(FILE_PATH, as_attachment=True) return "No file", 404 @app.route('/delete', methods=['GET']) def delete_file(): if os.path.exists(FILE_PATH): os.remove(FILE_PATH) return "Deleted" if __name__ == "__main__": app.run(host="0.0.0.0", port=2333, debug=True)