This repository was archived by the owner on Sep 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
61 lines (37 loc) · 1.28 KB
/
server.py
File metadata and controls
61 lines (37 loc) · 1.28 KB
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
import socket as network
from json import dumps, loads, decoder
import core.const as const
class Server:
def __init__(self):
self.name = network.gethostname()
self.ip = network.gethostbyname(self.name)
self.address = (self.ip, const.PORT)
def start(self):
with network.socket(network.AF_INET, network.SOCK_STREAM) as server:
print(f'[i] Binding {self.name} at {self.address}')
server.bind(self.address)
print('[i] Listening for client')
server.listen()
while True :
client, address = server.accept()
self.hanleUser(client, address)
def hanleUser(self, client, address):
print('.' * 50)
print(f'[+] User at {address} connected')
name = client.recv(const.BUFFER_SIZE)
name = name.decode()
print(f'[?] User authorized as {name}')
client.send(self.name.encode())
try:
connected = True
while connected:
data = client.recv(const.BUFFER_SIZE)
data = loads(data.decode())
if data == '!DICONNECT!' : connected = False
print(f'[u] {name}: {data}')
print(f'[s] {self.name}: 0')
client.send('0'.encode())
except decoder.JSONDecodeError:
print('[!] User forcibly stopped connection')
print(f'[-] User {name} at {address} disconnected')
Server().start()