import time from flask import Flask, request, redirect, render_template, session import _thread import socket import json
access = 0
app = Flask(__name__)
app.secret_key = 'QWERTYUIOP'# 对用户信息加密
defsocket_server(x): global access print('新线程已开启') s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.bind(('0.0.0.0', xxx)) s.listen() while (True): try: c, addr = s.accept() print('收到连接:'+str(addr)) if access == 1: print('门禁已开启') c.send('1'.encode('utf-8')) access = 0 else: c.send('0'.encode('utf-8')) c.close() except Exception as e: print(e)
@app.route('/', methods=['GET', "POST"]) deflogin(): global access d = open("dictionary.txt", "r") json_dictionary = d.read() d.close() dictionary = json.loads(json_dictionary) if request.method == 'GET': return render_template('login.html') user = request.form.get('user') pwd = request.form.get('pwd') reason = request.form.get('reason') time.sleep(1) if reason=='': return render_template('login.html', msg='事由不能为空') if user in dictionary and pwd == dictionary[user]: session['user_info'] = user access = 1 f = open("note.txt", "a") f.write("时间:"+str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))+",姓名:"+user+",事由:"+reason+"\n") f.close() print('用户'+user+'已登录') return redirect('/index') else: return render_template('login.html', msg='姓名或密码输入错误')
@app.route('/index') defindex(): global access user_info = session.get('user_info') ifnot user_info: return redirect('/login') return'The door is opened now! Welcome!'
@app.route('/logout') deflogout_(): del session['user_info'] return redirect('login')
if __name__ == "__main__": _thread.start_new_thread(socket_server, (0,)) app.run(host='0.0.0.0', port=5000, debug=False)
The domain name of this website has been changed to triority.cc(Using CDN via cloudflare, recommended) / www.triority.cc(Connecting directly, works better in Chinese mainland). Please contact me if you have any questions.