-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew_server.py
More file actions
30 lines (25 loc) · 830 Bytes
/
new_server.py
File metadata and controls
30 lines (25 loc) · 830 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
import socket
class NewServer:
def __init__(self, ip, port):
self.ip = ip
self.port = port
self.socket = None
def accept_connection(self):
try:
connection, address = self.socket.accept()
print("Accepted connection from", address)
return connection, address
except BlockingIOError:
return None, None
def start_listening(self):
self.is_running = True
self.socket = socket.create_server((self.ip, self.port))
self.socket.setblocking(False)
while self.is_running:
connection, address = self.accept_connection()
if not connection:
continue
connection.close()
self.socket.close()
def shutdown(self):
self.is_running = False