-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
62 lines (43 loc) · 1.71 KB
/
server.py
File metadata and controls
62 lines (43 loc) · 1.71 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
51
52
53
54
55
56
57
58
59
60
61
62
import socket
import threading
HEADER = 64
PORT = 5015
# SERVER = "192.168.56.10"
SERVER = "158.75.54.56" # for public server. 158.75.54.56 for wifi 192.168.56.10 158.75.87.169 for ethernet cable
# SERVER = socket.gethostbyname(socket.gethostname()) # for private server
ADDR = (SERVER, PORT)
FORMAT = "utf-8"
DISCONNECT_MESSAGE = "!DISCONNECT"
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
def handle_client(conn, addr):
print(f"[NEW CONNECTION] {addr} connected.")
connected = True
while connected:
msg_length = conn.recv(HEADER).decode(FORMAT)
# print(f"\n {msg_length} \n")
if msg_length:
msg_length = int (msg_length)
msg = conn.recv(msg_length).decode(FORMAT)
if msg == DISCONNECT_MESSAGE:
connected = False
else:
led_state = input("Enter the LED State Value (On 2/ Off 3): ")
if led_state == '':
led_state = 0
conn.send(bytes(f"{led_state}".encode(FORMAT)))
# conn.send(bytes(f"message received: {msg}\n".encode(FORMAT)))
print(f"[{addr}] {msg}")
conn.close()
def start():
server.listen()
print(f"[LISTENING] Server is listening on {SERVER}:{PORT}")
cheking = True
while cheking:
conn, addr = server.accept()
thread = threading.Thread(target=handle_client, args=(conn, addr))
thread.start()
# cheking = False
print(f"[ACTIVE CONNECTIONS] {threading.activeCount() - 1}")
print("[STARTING] server is starting...")
start()