-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
31 lines (26 loc) · 924 Bytes
/
Copy pathserver.py
File metadata and controls
31 lines (26 loc) · 924 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
import socket
import threading
def handle_client(client_socket):
while True:
data = client_socket.recv(1024)
if not data:
break
message = data.decode("utf-8")
print(f"Recieve message: {message}")
response = "Server recieved your message: " + message
client_socket.sendall(response.encode("utf-8"))
client_socket.close()
def main():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "127.0.0.1"
port = 12345
server_socket.bind((host, port))
server_socket.listen(5)
print(f"Server listening on {host}:{port}")
while True:
client_socket,client_address = server_socket.accept()
print(f"Accepted connection from: {client_address}")
client_handler = threading.Thread(target=handle_client, args=(client_socket,))
client_handler.start()
if __name__ == "__main__":
main()