def __init__(self, host=config.ip_address, port=config.mouse_server_port, mouse_speed=0.1): """Entry point of MouseServer, initializes the socket listener and contains the main loop""" self.MOUSE_MOVE_SPEED = mouse_speed self.sock = socket.socket() # Attempts to bind socket try: self.sock.bind((host, port)) except OSError: self.sock_error = self.ERROR_BIND return # Listens for one connection attempt at a time self.sock.listen(1) log("Listening on interface: {}:{}".format(host, port), context="MouseServer") # Continuously listen for connection attempts, and calls _on_connect() when one is made while True: conn, addr = self.sock.accept() log("Connection with {}:{} established".format(addr[0], addr[1]), context="MouseServer") self._on_connect(conn, addr)
def __del__(self): """Server termination house-keeping""" if self.sock_error: log("Exiting with error: {}".format( self._get_error(self.sock_error)), context="MouseServer") else: try: self.sock.send(self.RESPONSE_CLOSE) except OSError as msg: log("Failed to send: {}".format(msg), context="MouseServer") pass self.sock.close()
def __init__(self, host=config.ip_address, port=config.netplay_port): self.sock = socket() # Attempts to bind socket try: self.sock.bind((host, port)) except OSError: # self.sock_error = self.ERROR_BIND self.sock.close() return # Listens for one connection attempt at a time self.sock.listen(1) log("Listening on interface: {}:{}".format(host, port), context="NetLibListener") # Continuously listen for connection attempts, and calls _on_connect() when one is made while True: conn, addr = self.sock.accept() log("Connection with {}:{} established".format(addr[0], addr[1]), context="NetLibListener") self._on_connect(conn)
from pymsgbox import prompt, confirm from NetLibPlayer.common import add_torrent, log if __name__ == "__main__": magnet = prompt("Magnet Link:", "Add Torrent") if magnet: name = prompt("Torrent Name:", "Add Torrent") cat = confirm("Set category to:", "Add Torrent", ["Other", "Movie", "Show", "Cancel"]) or "Other" if not name or cat == "Cancel": log("Canceled. {}".format(magnet), important=True, context="Add Torrent") elif not add_torrent(magnet, name, cat): log("An error occurred. | Magnet='{}' | Name='{}'".format(magnet, name), important=True, context="Add Torrent")
def _on_connect(conn): """Connection with client has been established""" try: data = str(conn.recv(4096), "utf-8").strip().split(" ") args = "" file = "" root = "" new_name = "" cat = "" magnet = "" add = False lib = False play = False for cmd in data: if "=" in cmd: cmd_list = cmd.split("=") com, args = cmd_list[0], "=".join(cmd_list[1:]).replace( "`", " ") else: com = cmd if com == "root": root = args.strip('"').strip("'") elif com == "file": file = args.replace("`", " ") elif com == "list": lib = True elif com == "play": play = True elif com == "add": add = True elif com == "cat": cat = args elif com == "name": new_name = args elif com == "link": magnet = args if lib and root: [ conn.send( str.encode("{}@{}\n".format( cat.path.split("\\")[-1], "|".join(sorted(get_file_list(cat.path)))))) for cat in scandir(root) ] elif play and file.split(".")[-1] in config.allowed_extensions: run([config.vlc_path, file, "-f"]) elif add and cat and new_name and magnet: conn.send( b'OK' if add_torrent(magnet, new_name, cat) else b'BAD') except sock_error as error: log("Error: {}".format(error), context="NetLibListener") finally: conn.close()
def _on_connect(self, conn, remote): """Connection with client has been established""" # Initialize connection error value and send a connection start response conn_error = False conn.send(self.RESPONSE_START) while True: self.input_error = False msg_queue = "" # Waits for client to send something... try: data = conn.recv(1024) except socket.error: conn_error = self.ERROR_CON_DIED break # No data received, terminate connection if not data: conn_error = self.ERROR_CON_LOST break # Splits data by space character. command = data[0], args = data[1:] data = str(data, "utf-8").strip().split(" ") command = data[0] if len(data) == 2: args = data[1] else: args = None if command == "GOTO": if args is None: self.input_error = self.ERROR_BAD_ARGS else: coords = self.parse_coords(args) pag.moveTo(coords[0], coords[1], self.MOUSE_MOVE_SPEED) \ if type(coords[0]) is float else self._set_input_error(self.ERROR_BAD_ARGS) elif command == "MOVE": if args is None: self.input_error = self.ERROR_BAD_ARGS else: coords = self.parse_coords(args) pag.moveRel(coords[0], coords[1], self.MOUSE_MOVE_SPEED) \ if type(coords[0]) is float else self._set_input_error(self.ERROR_BAD_ARGS) elif command == "CLICK": pag.click() elif command == "DOWN": pag.mouseDown() elif command == "UP": pag.mouseUp() elif command == "VDOWN": if args is None: args = 1 pag.press("volumedown", int(args)) elif command == "VUP": if args is None: args = 1 pag.press("volumeup", int(args)) elif command == "VMUTE": pag.press("volumemute") elif command == "EXIT": pag.hotkey("alt", "f4") elif command == "SLEEP": log("Terminating connection with {}\nGoing to sleep...".format( remote[0]), context="MouseServer") conn.send(self.RESPONSE_CLOSE) conn.close() run_shell("{} -d -f -t 0".format(config.psshutdown)) break elif command == "RCLICK": pag.click(button="right") elif command == "SEND": if args is None: self.input_error = self.ERROR_BAD_ARGS else: if args == "8": key = "backspace" else: key = chr(int(args)) pag.press(key) elif command == "HELP": msg_queue = """\ Mouse Server v0.1 Available Commands: GOTO MOVE CLICK RCLICK SEND CLOSE HELP """ elif command == "CLOSE": log("Connection ended per client request", context="MouseServer") conn.send(self.RESPONSE_CLOSE) conn.close() break # Invalid command else: self.input_error = self.ERROR_BAD_COMMAND # Responds to client request with a 0 for OKAY and 1 for ERROR if self.input_error is not False: conn.send(self.RESPONSE_BAD + bytes(self._get_error(self.input_error), "UTF-8")) else: conn.send(self.RESPONSE_GOOD + bytes(msg_queue, "UTF-8")) # Connection ended, if there was an error report it if conn_error is not False: log("Input error: {}".format(self._get_error(conn_error)), context="MouseServer")