-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
35 lines (26 loc) · 933 Bytes
/
server.py
File metadata and controls
35 lines (26 loc) · 933 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
import socket
def start_server():
host = 'localhost'
port = 8000
# Create a socket object
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to a specific address and port
server_socket.bind((host, port))
# Listen for incoming connections
server_socket.listen(5)
print("Server started. Waiting for connections...")
while True:
# Accept a client connection
client_socket, addr = server_socket.accept()
print(f"Connected to {addr[0]}:{addr[1]}")
# Handle client messages
while True:
message = client_socket.recv(1024).decode()
if not message:
break
print(f"Received message: {message}")
client_socket.send("Message received".encode())
# Close the client connection
client_socket.close()
if __name__ == '__main__':
start_server()