-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
63 lines (54 loc) · 1.54 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
"""
A simple Python script to receive messages from a client over
Bluetooth using PyBluez (with Python 3.7).
"""
import code
#import subprocess
import bluetooth
import os
import time
from ports import *
hostMACAddress = '' #leave empty
port = 3
backlog = 1
size = 1024
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
s.bind((hostMACAddress, port))
s.listen(backlog)
vars = globals().copy()
vars.update(locals())
shell = code.InteractiveConsole(vars)
print("up and running")
def fileWrite(msg):
shell.push("import sys")
shell.push("stdoutOrigin = sys.stdout")
shell.push("sys.stdout = open(\"log.txt\", \"w\")")
shell.push(msg)
shell.push("sys.stdout.close()")
shell.push("sys.stdout=stdoutOrigin")
try:
client, clientInfo = s.accept()
while 1:
data = client.recv(size)
if data:
msg = data.decode()
if msg != "ignore":
fileWrite(msg)
sensor_name = msg[0:1]
try:
f = open("log.txt", 'r')
line = f.readline().rstrip()
#print(line)
if (os.path.getsize("log.txt") == 0):
client.send("ignore") # Echo back to client
else:
client.send(sensor_name + line)
f.close()
except:
client.send("ignore")
else:
client.send("ignore")
except:
print("Closing socket")
client.close()
s.close()