-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
50 lines (38 loc) · 1.46 KB
/
server.py
File metadata and controls
50 lines (38 loc) · 1.46 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
import socket
import threading
class Server:
host = '127.0.0.1'
port = 34191 # Use an appropriate localhost port: see https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers for known used ports
def __init__(self):
# Specify IPv4 and TCP
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.bind((self.host, self.port))
self.server.listen(5) # Allow max 5 connection attempts waiting in queue
self.running = True
self.thread = threading.Thread(target=self.listen_for_connections, daemon=True)
self.thread.start()
def listen_for_connections(self):
while self.running:
client, _ = self.server.accept()
# Read data
data = b""
chunk = client.recv(1024) # Read up to 1024 bytes of data
while chunk: # Continue reading until no more data is received
data += chunk
chunk = client.recv(1024)
self.process_data(data.decode())
client.close()
def process_data(self, data):
print("Data received:", data)
def stop(self):
self.running = False
self.server.close()
if __name__ == '__main__':
server = Server()
import time
import random
while True:
time.sleep(1)
print("doing stuff", random.randint(1, 100)) # main thread isn't blocked
# input("Press Enter to stop the server...")
# server.stop()