-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
37 lines (28 loc) · 849 Bytes
/
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
import socket
import time
HOST = '0.0.0.0'
PORT = 65432
def main():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((HOST, PORT))
server_socket.listen(1)
print(f"Server listening on port {PORT}")
while True:
client_socket, client_address = server_socket.accept()
print(f"Connection from {client_address}")
try:
handle_client(client_socket)
except Exception as e:
print(f"Error: {e}")
finally:
client_socket.close()
def handle_client(client_socket):
while True:
data = client_socket.recv(1024).decode('utf-8')
if not data:
break
print(f"{time.strftime('%H:%M:%S')}: {data}")
if data == "/close":
break
if __name__ == '__main__':
main()