-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
38 lines (36 loc) · 1.36 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
import socket
import os
import time
port = 54321
if __name__ == '__main__':
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(('0.0.0.0', port))
s.listen()
while True:
con, addr = s.accept()
data = b''
with con:
while True:
d = con.recv(1)
if not d: break
data += d
if d == '\n': break
data = data.decode("utf-8")
mac_address = data.split(',')[0]
file_name = mac_address.replace(':', '_') + '.txt'
if not os.path.isfile(file_name):
with open(file_name, 'w') as f:
f.write('#Data from sensor'
' with mac address %s\n' % mac_address)
f.write('#Human Time, '
'Timestamp (s), '
'Temperature (°C), '
'Relative Humidity (%)\n')
data = ', '.join(data.split(',')[1:])
with open(file_name, 'a') as f:
f.write('%s, %s, %s' % (time.asctime(),
round(time.time()),
data))
print(time.asctime(), end='\n ')
print(mac_address, end='\n ')
print(data)