-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
56 lines (46 loc) · 2.08 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
import socket
import threading
HEADER = 64
PORT = 5050
SERVER = socket.gethostbyname(socket.gethostname()) # IP address of the local server
ADDR = (SERVER, PORT)
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!EXIT"
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # create a socket object
server.bind(ADDR) # bind the socket to the address
c_message = ""
c_addr = ""
def handle_client(conn, addr):
print(f"[NEW CONNECTION] {addr} connected.")
global c_message
global c_addr
connected = True
while connected:
msg_length = conn.recv(HEADER).decode(FORMAT) # receive the length of the message
if msg_length:
msg_length = int(msg_length)
msg = conn.recv(msg_length).decode(FORMAT) # receive the message
if msg == DISCONNECT_MESSAGE:
connected = False
if c_message == "":
pass
else:
c_message = f"[{c_addr}] {c_message}"
conn.send(c_message.encode(FORMAT)) # send the message to the client
c_message = ""
c_addr = ""
print(f"[{addr}] {msg}")
conn.send("Msg received".encode(FORMAT))
c_message = msg
c_addr = addr
conn.close() # close the connection
def start():
server.listen() # start listening for connections
print(f"[LISTENING] Server is listening on {SERVER}")
while True:
conn, addr = server.accept() # accept a connection
thread = threading.Thread(target=handle_client, args=(conn, addr)) # create a thread for each client
thread.start()
print(f"[ACTIVE CONNECTIONS] {threading.active_count() - 1}") # print the number of active connections
print("[STARTING] server is starting...")
start()