-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
37 lines (28 loc) · 882 Bytes
/
Copy pathserver.py
File metadata and controls
37 lines (28 loc) · 882 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
36
37
import socket
HOST = "localhost"
PORT = 12345
BUFFER_SIZE = 1024
def main():
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
server.bind((HOST, PORT))
server.listen(5)
print(f"Server listening on {HOST}:{PORT}...")
conn, addr = server.accept()
print(f"Connected by {addr}")
try:
buf = conn.recv(BUFFER_SIZE).decode()
print(f"Received: {buf}")
conn.send("got it".encode())
except (ConnectionResetError, UnicodeDecodeError) as e:
print(f"Communication error: {e}")
finally:
conn.close()
print("Connection closed.")
except OSError as e:
print(f"Server error: {e}")
finally:
server.close()
if __name__ == "__main__":
main()