-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_server.py
69 lines (57 loc) · 1.82 KB
/
test_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
66
67
68
69
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "artsin, sashkoiv"
__copyright__ = "Copyright 2023, KyivHacklab"
__credits__ = ["artsin, sashkoiv, paulftw, lazer_ninja, Vova Stelmashchuk"]
import socket
import sys
import json
MY_IP = '192.168.88.220'
READER_PORT = 9999
db = {
"u4xf5xfb+xc2x94/x11xa7x05]xc4?xa6xf9xcdQxf0xfbx83xc7xe4hxf8xcdxbaxe7xa4rxdcf":"grant",
"{xecx81xe1xbax86x92xf1x9cx82x122Qx0fxd8&xc0x96YxccSxafx13(xa3x8dx1bx04x1ex03xe3g":"deny"
}
response = {
"type": "response",
"result": ""
}
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
server_address = (MY_IP, READER_PORT)
print('starting up on {} port {}'.format(*server_address))
sock.bind(server_address)
# Listen for incoming connections
sock.listen(1)
data = ""
while True:
# Wait for a connection
print('waiting for a connection')
connection, client_address = sock.accept()
# connection.timeout(10)
try:
print('connection from', client_address)
# Receive the data in small chunks and retransmit it
while True:
data += connection.recv(100).decode()
if '\r\n' in data:
try:
key = json.loads(data)['key']
res = db[key]
except:
res = 'usernotfound'
response['result'] = res
# data
print('sending data back to the client')
connection.sendall((json.dumps(response)+'\r\n').encode('utf-8'))
print(json.dumps(response).encode('utf-8'))
data = ""
break
elif not data:
break
connection.close()
print("Here")
finally:
# Clean up the connection
connection.close()