-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
50 lines (36 loc) · 1.48 KB
/
server.py
File metadata and controls
50 lines (36 loc) · 1.48 KB
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
40
41
42
43
44
45
46
47
48
49
50
import socket
def start_server():
# Create TCP/IP socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
server_address = ('localhost', 5000)
server_socket.bind(server_address)
# Listen for incoming connections
server_socket.listen(1)
print("Server started on localhost:5000")
while True:
print("Waiting for connections...")
client_socket, client_address = server_socket.accept()
try:
print(f"Client connected from {client_address}")
while True:
# Recieve data
data = client_socket.recv(1024).decode('utf-8')
if not data:
break
print(f"Message received: {data}")
# Verify message: "DESCONEXION"
if data == "DESCONEXION":
print("Client requested disconnection")
break
# Convert message to uppercase and send response
response = data.upper()
client_socket.send(response.encode('utf-8'))
finally:
print("Closing connection with client")
client_socket.close()
if __name__ == "__main__":
try:
start_server()
except Exception as e:
print(f"Error: {e}")