-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_server.py
79 lines (62 loc) · 2.59 KB
/
test_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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import socket
import threading
# Define the server addresses and ports
HOST = "192.168.1.132" # Change this to your specific IP address
PORT1 = 8080 # First port
PORT2 = 8081 # Second port
# TCP command bytes
TCP_ON_DATA = b"\x01"
TCP_OFF_DATA = b"\x00"
TCP_GET_STATE = b"\x03"
# Shared states for both servers
state_8080 = TCP_OFF_DATA
state_8081 = TCP_OFF_DATA
def handle_client(client_socket, client_address, port):
global state_8080, state_8081
# print(f"Connected by {client_address} on port {port}")
# Receive data from the client
data = client_socket.recv(1024)
if port == PORT1:
if data == TCP_GET_STATE:
# print(f"Port {port}: Received TCP_GET_STATE, sending {state_8080}")
client_socket.sendall(state_8080)
elif data == TCP_ON_DATA:
state_8080 = TCP_ON_DATA
print(f"Port {port}: Received TCP_ON_DATA, state updated to ON")
elif data == TCP_OFF_DATA:
state_8080 = TCP_OFF_DATA
print(f"Port {port}: Received TCP_OFF_DATA, state updated to OFF")
elif port == PORT2:
if data == TCP_GET_STATE:
# print(f"Port {port}: Received TCP_GET_STATE, sending {state_8081}")
client_socket.sendall(state_8081)
elif data == TCP_ON_DATA:
state_8081 = TCP_ON_DATA
print(f"Port {port}: Received TCP_ON_DATA, state updated to ON")
elif data == TCP_OFF_DATA:
state_8081 = TCP_OFF_DATA
print(f"Port {port}: Received TCP_OFF_DATA, state updated to OFF")
else:
print(f"Port {port}: Received {data}, no response sent")
# Close the connection
client_socket.close()
def start_tcp_server(port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
# Bind the socket to the address and port
server_socket.bind((HOST, port))
# Start listening for incoming connections
server_socket.listen()
print(f"Server listening on {HOST}:{port}")
while True:
# Accept a connection from a client
client_socket, client_address = server_socket.accept()
with client_socket:
handle_client(client_socket, client_address, port)
if __name__ == "__main__":
# Start two threads for the two servers
threading.Thread(target=start_tcp_server, args=(PORT1,), daemon=True).start()
threading.Thread(target=start_tcp_server, args=(PORT2,), daemon=True).start()
# Keep the main thread alive to keep the servers running
print("Servers are running. Press Ctrl+C to stop.")
while True:
pass