-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_server.py
More file actions
39 lines (24 loc) · 902 Bytes
/
simple_server.py
File metadata and controls
39 lines (24 loc) · 902 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
38
39
import socket
def start_server():
host = "localhost"
port = 9000 # 一般要大于5000
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(host, port)
server_socket.listen(5)
print("Server is running and waiting for connection.")
try:
while True:
client_socket, ip_add = server_socket.accept()
print("Client connected.")
message = client_socket.recv(1024).decode()
print("Client says: ", message)
response = "Message received by server."
client_socket.sendall(response.encode())
client_socket.close()
except KeyboardInterrupt:
print("Shutting down server.")
finally:
server_socket.close()
if __name__ == "__main__":
start_server()