def execute_file(self): commands = [] commands[0] = storage.get(storage.KEY_COMMAND_PART_I) commands[1] = statics.PROGRAM_PATH + storage.get(storage.KEY_FILE_NAME) commands[2] = storage.get(storage.KEY_COMMAND_PART_II) # TODO: CWD needed? FOR WORKING WITH FILE? self.process = Popen(commands, stdout=PIPE, stderr=PIPE, bufsize=1) # https://stackoverflow.com/questions/31833897/python-read-from-subprocess-stdout-and-stderr-separately-while-preserving-order threading.Thread(target=self.handle_error_stream, args=[self.process.stderr]).start() threading.Thread(target=self.handle_output_stream, args=[self.process.stdout]).start()
def receive_broadcast(self): while True: # TODO: Refactore message = self.broadcast_socket.recvfrom(1024) print(message[0]) tcp_socket = socket.socket() ip = storage.get(storage.KEY_SERVER_IP) port = storage.get(storage.KEY_SERVER_PORT) tcp_socket.connect((ip, port)) # print('Got connection from ', (ip, port)) self.connection = con.Connection(ip, port, tcp_socket)
def __init__(self): self.process = None self.connection = None # https://stackoverflow.com/questions/22878625/receiving-broadcast-packets-in-python self.broadcast_socket = socket(AF_INET, SOCK_DGRAM) ip = storage.get(storage.KEY_SERVER_IP) port = storage.get(storage.KEY_BROADCAST_PORT) self.broadcast_socket.bind((ip, port)) # Wait for new sessions in background thread = threading.Thread(target=self.receive_broadcast, args=()) thread.daemon = True thread.start()
def report(report_id: str): if request.args.get('force_regen') == 'true': file_rsp = None else: file_rsp = storage.get(report_id) if file_rsp is None: rsp: requests.Response = requests.post(get_gen_url(report_id)) assert rsp.status_code == HTTPStatus.OK, (rsp.request.url, rsp.content.decode()) file_rsp = storage.get(rsp.json()) return Response(file_rsp.stream(32 * 1024), headers=dict(file_rsp.headers), direct_passthrough=True)
def handle_command(self, command): # Check for Commands, some may contain params if command == statics.COMMAND_SEND_FILE: filename = storage.get(storage.KEY_FILE_PATH) self.send_file(filename) elif command == statics.COMMAND_EXIT: self.sock.close()
def send_broadcast(self): """ Notify all clients via broadcast to start a new session """ # https://stackoverflow.com/questions/22878625/receiving-broadcast-packets-in-python self.broadcast_socket.setsockopt(SOL_SOCKET, SO_BROADCAST, 1) port = storage.get(storage.KEY_BROADCAST_PORT) message = statics.COMMAND_NEW_SESSION + self.session_token self.broadcast_socket.sendto(message, ('255.255.255.255', port))
def create_session_token(self): """ Create a unique string so every client will notice which session should be running now """ self.session_token = storage.get(storage.KEY_SESSION_TOKEN) if self.session_token == "": self.session_token = "".join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10)) self.session_token = hash(self.session_token) storage.put(storage.KEY_SESSION_TOKEN, self.session_token)
def __init__(self): self.connections = [] self.session_token = None # Init broadcast socket for sending self.broadcast_socket = socket(AF_INET, SOCK_DGRAM) # Init tcp socket for listening for clients to connect self.tcp_socket = socket.socket() port = storage.get(storage.KEY_SERVER_PORT) self.tcp_socket.bind((socket.gethostname(), port)) # Wait for connections in background thread = Thread(target=self.listen_for_clients, args=()) thread.daemon = True thread.start() # When server is started release file self.release_file()
def handle_error_stream(self, stream): try: with stream: for line in iter(stream.readline, b''): # Extract State message start = line.find(statics.MESSAGE_PREFIX) end = line.find(statics.MESSAGE_POSTFIX) # State message found if start != -1 != end: # Send to server with client id self.connection.send_command(statics.COMMAND_UPDATE_STATE , storage.get(storage.KEY_CLIENT_ID) , line[start: end]) print("State:" + line[start: end]) finally: print( "Error" )