-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
35 lines (28 loc) · 945 Bytes
/
server.py
File metadata and controls
35 lines (28 loc) · 945 Bytes
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
import socket
import threading
SERVER = socket.gethostbyname(socket.gethostname())
PORT = 4040
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((SERVER, PORT))
def handle_client(conn, addr):
print(f"Connect by {addr}")
connection = True
while connection:
msg_length = conn.recv(64).decode('utf-8')
if msg_length:
msg_length = int(msg_length)
msg = conn.recv(msg_length).decode('utf-8')
if msg == "!DISCONNECT":
connection = False
print(f"{addr} messages = {msg}")
conn.send("Thank you for communicate to server!".encode('utf-8'))
conn.close()
def start():
s.listen()
while True:
conn, addr = s.accept()
thread = threading.Thread(target=handle_client, args=(conn, addr))
thread.start()
print(f"Connection new {threading.active_count() - 1}")
print("SERVER READY")
start()