-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_server.py
34 lines (26 loc) · 1.03 KB
/
tcp_server.py
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
import socket
def start_server(host='0.0.0.0', port=8080):
# Step 1: Create a socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Step 2: Bind the socket to an address and port
server_socket.bind((host, port))
# Step 3: Listen for incoming connections
server_socket.listen(1)
print(f"Server is listening on {host}:{port}")
while True:
try:
# Step 4: Accept a connection
client_socket, client_address = server_socket.accept()
# Step 5: Log the client's IP address
print(f"Client connected from {client_address[0]}:{client_address[1]}")
# Step 6: Close the connection
client_socket.close()
except KeyboardInterrupt:
print("\nShutting down the server...")
break
except Exception as e:
print(f"An error occurred: {e}")
# Step 7: Close the server socket
server_socket.close()
if __name__ == "__main__":
start_server()