-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
43 lines (32 loc) · 1.47 KB
/
server.py
File metadata and controls
43 lines (32 loc) · 1.47 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
import socket
import threading
HEADER = 64
PORT = 5050
SERVER = socket.gethostbyname(socket.gethostname())
ADDR = (SERVER, PORT)
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = '!DISCONNECT'
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
def handle_client(conn, addr):
print("[NEW CONNECTION] {0} connected.".format(addr)) #print(f"[NEW CONNECTION] {addr} connected.") #Python 3
connected = True
while connected:
msg_length = conn.recv(HEADER).decode(FORMAT)
if msg_length:
msg_length = int(msg_length)
msg = conn.recv(msg_length).decode(FORMAT)
if msg == DISCONNECT_MESSAGE:
connected = False
print("[{0}] {1}".format(addr,msg)) #print(f"[{addr}] {msg}") #Python 3
conn.close()
def start():
server.listen(PORT) #server.listen() # pe Python 3 nu tb specificat si PORT
print("[LISTENING] Server is listening on {0}".format(SERVER)) #print(f"[LISTENING] Server is listening on {SERVER}") #Python 3
while True:
conn, addr = server.accept()
thread = threading.Thread(target = handle_client, args=(conn, addr))
thread.start()
print("\n[ACTIVE CONNECTIONS] {0}".format(threading.activeCount() -1)) #print(f"[ACTIVE CONNECTIONS] {threading.activeCount() -1}") #Python 3
print("[STARTING] server is starting...")
start()