-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
65 lines (57 loc) · 2.01 KB
/
server.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
import socket
import mysql.connector
from setup import *
db = mysql.connector.connect(user=USER, password=PWD,
host='127.0.0.1',
port=DB_PORT,
database=DB_NAME)
cursor = db.cursor()
host = HOST
port = 8085
isAuthorized = bytearray([0])
isUserInDB = bytearray([0])
authorizedUser = ""
def receiveReq():
res = conn.recv(1024)
res = res.decode()
return res
def queryDB(cursor, query):
userReq = receiveReq()
query = query %userReq
cursor.execute(query)
dbRes = cursor.fetchall()
return dbRes
def login():
query = 'SELECT * FROM `users` WHERE username = "%s"'
dbResponse = queryDB(cursor, query)
if len(dbResponse) > 0:
isUserInDB = bytearray([1])
conn.sendall(isUserInDB)
if isUserInDB == b'\x01':
query = 'SELECT * FROM `users` WHERE password = "%s"'
dbResponse = queryDB(cursor, query)
if len(dbResponse) > 0:
isAuthorized = bytearray([1])
authorizedUser = dbResponse[0][0]
conn.sendall(isAuthorized)
return authorizedUser
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.bind((host, port))
sock.listen(1)
conn, addr = sock.accept()
with conn:
print('connected by: ', addr)
while isUserInDB == b'\x00':
authorizedUser = login()
isAuthorized = b'\x01' if authorizedUser > 0 else b'\x00'
isUserInDB = isAuthorized if isAuthorized == b'\x01' else b'\x00'
while isAuthorized == b'\x01':
message = receiveReq()
query = f'INSERT INTO messages (id_user, message, ip) VALUES({authorizedUser}, "{message}", "{addr[0]}")'
cursor.execute(query)
response = cursor.fetchall()
db.commit()
conn.sendall(b'Message was sent to the Data Base')
if not message:
sock.close()
break