-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
70 lines (65 loc) · 2.44 KB
/
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
import socket
import time
import threading
HOST = '' # Symbolic name meaning all available interfaces
PORT = 50002 # Arbitrary non-privileged port
temp_optimal = '25'
hum_optimal = '50'
co2_optimal = '700'
def newConnection(socket, addr):
while(True):
try:
data = conn.recv(1024)
if not data: break
print(data.decode("utf-8"))
if data.decode("utf-8") == 'SEND MEMES':
conn.sendall(temp_optimal.encode('ascii'))
time.sleep(1)
conn.sendall(hum_optimal.encode('ascii'))
time.sleep(1)
conn.sendall(co2_optimal.encode('ascii'))
if data.decode("utf-8") == 'LT':
# Low Temperature
print("LT")
conn.sendall("ok".encode('ascii'))
if data.decode("utf-8") == 'HT':
# High Temperature
print("HT")
conn.sendall("ok".encode('ascii'))
if data.decode("utf-8") == 'LH':
# Low Humidity
print("LH")
conn.sendall("ok".encode('ascii'))
if data.decode("utf-8") == 'HH':
# High Humidity
print("HH")
conn.sendall("ok".encode('ascii'))
if data.decode("utf-8") == 'HC':
# High Carbon
print("HC")
conn.sendall("ok".encode('ascii'))
if data.decode("utf-8") == 'TO':
# High Carbon
print("TO")
conn.sendall("ok".encode('ascii'))
if data.decode("utf-8") == 'HO':
# High Carbon
print("HO")
conn.sendall("ok".encode('ascii'))
if data.decode("utf-8") == 'CO':
# High Carbon
print("CO")
conn.sendall("ok".encode('ascii'))
except socket.timeout:
print("Timeout con " + addr + ", STACCA STACCA STACCAAAHAHHHAAHH")
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen(1)
while(True):
conn, addr = s.accept()
t = threading.Thread(target=newConnection, args=(conn,addr), name="Thread")
t.start()
#Note it's (addr,) not (addr) because second parameter is a tuple
#Edit: (c,addr)
#that's how you pass arguments to functions when creating new threads using thread module.
s.close()