forked from BrettMayson/Lir
/
server.py
executable file
·89 lines (71 loc) · 2.26 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3.4
HOST = ''
import socket
import sys
import fs,os
#TODO replace with threading module
from _thread import *
import subprocess
import settings
def resp(conn):
conn.send(b'Lir Server Connection\nSend a command!\n')
#infinite loop so that function do not terminate and thread do not end.
while True:
#Receiving from client
#TODO replace with buffer to receive large commands
data = conn.recv(1024)
data = str(data)[2:-1]
#cleanup telnet or other methods of input
if data.endswith("\\r\\n"):
data = data[:-4]
#android adds newline
if data.endswith("\\n"):
data = data[:-2]
print("RECEIVED:",data)
reply = b'OK;'
try:
mode,data = data.split(":",1)
except:
mode = "speech"
if mode == "speech":
command = subprocess.getoutput(fs.home()+"/.lir/dictionary \""+data+"\" "+fs.home()+"/.lir/actions/default.dic")
elif mode == "direct":
command = data
else:
reply += b"UnknownMode;"
d = os.getcwd()
os.chdir(fs.home()+"/.lir/bin")
print("EXECUTING:",command)
out = subprocess.getoutput("./"+command)+";"
reply += out.encode('utf-8')
os.chdir(d)
if not data:
break
try:
conn.sendall(reply + b"\n")
except:
#client disconnected immediatly after submitting command
pass
#came out of loop
conn.close()
def main():
info = settings.ini(fs.home()+"/.lir/main.ini")
PORT = int(info.get("server","port"))
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print("Created Socket")
try:
s.bind((HOST,PORT))
except socket.error as e:
print ("Bind Failed.",e)
return False
print ("Bind Complete.")
s.listen(10)
print ("Socket Listening on port " + str(PORT))
listen = True
while listen:
con,addr = s.accept()
print('Connected with ' + addr[0] + ':' + str(addr[1]))
start_new_thread(resp ,(con,))
if __name__ == "__main__":
main()