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,))
import os import time import datetime import fs # Install by `pip install pyfs` import re ROOT_DIR = fs.home() MONITORING_ROOT_DIR = 'Dropbox/' TARGET_ROOT_DIR = 'Dropbox/_Archives' LOG_DIR = 'Dropbox/_Archives/Log' BACKUP_DIR_SUFFIX = '_archives' ELAPSED_TIME = 7776000 #7776000 seconds = 90 days def _remove_test_dir(ROOT_DIR, TARGET_ROOT_DIR, backup_dir_name): backup_dir_name = fs.join([ROOT_DIR, TARGET_ROOT_DIR, backup_dir_name]) try: fs.rmdir(backup_dir_name, recursive=True) print (backup_dir_name, "was successfully removed") except: print ("ERROR", backup_dir_name, "couldn't removed by func:_remove_test_dir") def get_monitoring_dir(ROOT_DIR, MONITORING_ROOT_DIR): return fs.join([ROOT_DIR, MONITORING_ROOT_DIR]) def get_backup_dir_list(monitoring_dir, ELAPSED_TIME): backup_dir_list = [] now = int(time.time()) for f in fs.find('*', path=monitoring_dir): last_accessed = fs.stat(f).st_atime diff = now - last_accessed if diff > ELAPSED_TIME: