-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
111 lines (76 loc) · 2.34 KB
/
index.py
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
109
110
111
import hashlib
from dataclasses import dataclass
from flask import Flask, render_template, request, jsonify
from flask_mysqldb import MySQL
from flask_socketio import SocketIO, send
@dataclass
class User:
id: int
username: str
app = Flask(__name__)
app.config['SECRET_KEY'] = 'vnkdjnfjknfl1232#'
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = 'mochat'
mysql = MySQL(app)
socketIo = SocketIO(app)
@app.route('/')
def login():
return render_template("login.html")
@app.route('/home')
def home():
return render_template("home.html")
@app.route('/chat', methods=['POST', 'GET'])
def chat():
if request.method == 'GET':
return render_template("chat.html")
def md5(password):
md5 = hashlib.md5(password.encode()).hexdigest()
return md5
def messageReceived():
print('message was received!!!')
@socketIo.on('message')
def handleMessage(msg):
send(msg, broadcast=True)
@app.route('/api/login')
def loginApi():
name = request.values['username']
md = md5(request.values['password'])
cursor = mysql.connection.cursor()
cursor.execute(''' SELECT * from user WHERE user_name = %s AND password = %s''', (name, md))
mysql.connection.commit()
records = cursor.fetchall()
user = (0, "NotFound!")
for row in records:
user = User(row[0], row[1])
cursor.close()
return jsonify(user)
@app.route('/api/register', methods=['POST'])
def singUpApi():
print("register")
print(request.form)
name = request.form['username']
md = md5(request.form['password'])
cursor = mysql.connection.cursor()
cursor.execute('''INSERT INTO user(user_name, password) VALUES(%s, %s)''', (name, md))
mysql.connection.commit()
user = (name, md)
if cursor.rowcount > 0: