-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoscommand.py
More file actions
executable file
·117 lines (98 loc) · 2.85 KB
/
oscommand.py
File metadata and controls
executable file
·117 lines (98 loc) · 2.85 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""
OsCommand: remote command server
by: zappfinger (Richard van Bemmelen)
version: 1.2
date: 24-11-2018
1.1 added delete option
1.2 removed command.py dependency
"""
from pythonosc import dispatcher
from pythonosc import osc_server, udp_client
import os, time
from os import listdir
from os.path import isfile, join
from DBclass import *
import threading
from queue import Queue
from subprocess import Popen, PIPE, STDOUT
import json
"""
enter the name of the SQLite client machine to connect to
"""
##########################
#name = 'Macbook'
name = 'Pi3B+'
##########################
conf = db()
otherIP = conf.select('select IP from nodes where name="%s" ' % name)[0][0]
q = Queue()
class server():
def __init__(self, ip, port):
self.dispatcher = dispatcher.Dispatcher()
self.dispatcher.map("/command", self.command_handler, "")
self.dispatcher.map("/SQLcommand", self.SQLcommand_handler, "")
self.server = osc_server.ThreadingOSCUDPServer((ip, port), self.dispatcher)
def command_handler(self, unused_addr, args, cmdtext):
print("{0}".format(cmdtext))
if 'cd' in cmdtext:
os.chdir(cmdtext.split()[1])
res = os.getcwd()
else:
res=[]
with Popen(cmdtext, stdout=PIPE, stderr=STDOUT, shell=True, universal_newlines=True) as process:
for line in process.stdout:
res.append(line)
print(res)
q.put(res)
def SQLcommand_handler(self, unused_addr, args, qrytext):
print("{0}".format(qrytext))
res = ''
if 'SELECT' in qrytext or 'select' in qrytext:
res = db.select(qrytext)
elif 'CREATE' in qrytext or 'create' in qrytext:
res = db.exec(qrytext)
elif 'INSERT' in qrytext or 'insert' in qrytext:
res = db.exec(qrytext)
elif 'UPDATE' in qrytext or 'update' in qrytext:
res = db.exec(qrytext)
elif 'DELETE' in qrytext or 'delete' in qrytext:
res = db.exec(qrytext)
#if len(res)==0:
# res='OK'
print(res)
q.put(res)
class client():
def __init__(self, ip, port):
self.client = udp_client.SimpleUDPClient(ip, port)
def send(self):
while 1:
rep = q.get()
self.client.send_message("/reply", json.dumps(rep))
print(json.dumps(rep))
time.sleep(1)
if __name__ == "__main__":
# select db to connect to
print('Opening config database')
conf = db()
dbname = conf.select('select lastDB from persist')[0][0]
print('lastDB from persist: %s' % dbname)
db = db(dbname)
#print('connecting to remote database %s' % dbname)
print('starting SQLite server')
#
def worker1():
serv = server('0.0.0.0', 8889)
print("Serving on {}".format(serv.server.server_address))
serv.server.serve_forever()
def worker2():
clint = client(otherIP, 8889)
clint.send()
thread_list = []
thread1 = threading.Thread(target=worker1)
thread_list.append(thread1)
thread1.start()
thread2 = threading.Thread(target=worker2)
thread_list.append(thread2)
thread2.start()
while 1:
time.sleep(.5) # uses less CPU than pass