class CountApiServer(threading.Thread): def __init__(self, port, parent): super(CountApiServer, self).__init__() self.parent = parent self.__listener = Listener(('127.0.0.1', port)) def run(self): conn = self.__listener.accept() while 1: if conn.closed: conn = self.__listener.accept() try: msg = conn.recv() except EOFError: conn.close() continue else: if msg == 'quit': conn.close() break conn.send(self.parent.key_count) self.__listener.close() self.parent.stop()
class PolicyHandler(): def __init__(self,event_queue,ready_queue,sdx): self.event_queue = event_queue self.ready_queue = ready_queue self.sdx = sdx self.listener = Listener(('localhost', 6999), authkey=None) def start(self): while True: self.conn = self.listener.accept() print 'Policy Handler: Connection accepted from', self.listener.last_accepted tmp = self.conn.recv() data = json.loads(tmp) print data, type(data) print data.keys() print self.sdx.policy_status.keys() for pname in data: self.sdx.policy_status[pname] = int(data[pname]) print data, self.sdx.policy_status self.conn.send('Policy Handler: Hello World') self.conn.close() self.event_queue.put('Policy Handler: Policy Status changed for ')
class Server: def __init__(self, in_file): self.pids = set([]) self.pids.add(os.getppid()) self.address = ('localhost', 6000) self.listener = Listener(self.address, authkey='s'.encode(encoding='UTF-8')) self.shell_file = os.environ['HOME'] + '/.pyplayer' self.player = Player(in_file) self.interface = Interface(self.player) def run(self): cont = True watcher = Watcher(self.interface, self.shell_file, self.pids) watcher.start() while cont: conn = self.listener.accept() print('Connection from', self.listener.last_accepted) msg = conn.recv() print(msg) self.pids.add(msg['pid']) cont, info = self.interface.process_msg(msg['args']) watcher.notify_changes() conn.send(info) conn.close() watcher.stop() watcher.join()
def rpc(self, dest, method, args=None, kwargs=None): """ Make an RPC call to the named subscriber, expecting a response. This opens a :class:`multiprocessing.connection.Listener` and passes the Listener address to the child as part of the RPC call, so that the child can connect to the Listener to submit its results. Listeners are reused when possible to minimize overhead. """ try: listener = self._available_listeners.get_nowait() self.logger.debug("Reusing existing RPC listener at %s" % listener.address) except Empty: listener = Listener() self.logger.debug("Created new RPC listener at %s" % listener.address) self._blocking_listeners.append(listener) try: self._queues[dest].put((listener.address, (method, args or [], kwargs or dict()))) conn = listener.accept() self._blocking_listeners.remove(listener) try: while not self._terminate.is_set(): if conn.poll(self.poll_wait): return conn.recv() finally: conn.close() finally: self._available_listeners.put(listener)
def rpc_server(handler, address, authkey): sock = Listener(address, authkey=authkey) while True: client = sock.accept() t = Thread(target=handler.handle_connection, args=(client,)) t.daemon = True t.start()
class PctrlListener(object): def __init__(self): logger.info("Initializing the BGP PctrlListener") self.listener = Listener(config.ah_socket, authkey=None, backlog=128) logger.debug('SOCKSTAT ' + str(self.listener)) self.run = True def start(self): logger.info("Starting the BGP PctrlListener") while self.run: conn = self.listener.accept() logger.debug('SOCKSTAT ' + str(self.listener.last_accepted)) pc = PctrlClient(conn, self.listener.last_accepted) t = Thread(target=pc.start) with clientPoolLock: logger.debug('Trace: PctrlListener.start: clientActivePool before: %s', clientActivePool) logger.debug('Trace: PctrlListener.start: clientDeadPool before: %s', clientDeadPool) clientActivePool[pc] = t # while here, join dead threads. while clientDeadPool: clientDeadPool.pop().join() logger.debug('Trace: PctrlListener.start: clientActivePool after: %s', clientActivePool) logger.debug('Trace: PctrlListener.start: clientDeadPool after: %s', clientDeadPool) t.start() def stop(self): logger.info("Stopping PctrlListener.") self.run = False
def main(): _setproctitle('structurarium.graph') parser = argparse.ArgumentParser(description='Run Structurarium graph server') parser.add_argument('--version', '-v', action='version', version=__version__) parser.add_argument('host') parser.add_argument('port', type=int) parser.add_argument('path') parser.add_argument('--authkey', '-k', action='store') parser.add_argument( '--worker', '-w', action='store', type=int, help='default is set to the number of CPU' ) args = parser.parse_args() listener = Listener((args.host, args.port), family='AF_INET') database = Graph(args.path, authkey=args.authkey) pool = Pool(processes=args.worker) print 'Running on %s:%s' % (args.host, args.port) while True: connection = listener.accept() connection = reduce_connection(connection) # pool.apply_async(connect, [database, connection]) connect(database, connection)
def main(): setproctitle('graph4py.rpc') parser = argparse.ArgumentParser(description='Run Graph4Py graph server') parser.add_argument( '--version', '-v', action='version', version=__version__ ) parser.add_argument('host') parser.add_argument('port', type=int) parser.add_argument('backend') parser.add_argument('path') parser.add_argument('--authkey', '-k', action='store') parser.add_argument('--graph', '-g', action='store') args = parser.parse_args() graph_class = import_object(args.graph) if args.graph else None database = Graph4Py(args.backend, args.path, args.authkey, graph_class) def signal_handler(s, frame): database.close() sys.exit(0) signal.signal(signal.SIGINT, signal_handler) listener = Listener((args.host, args.port), family='AF_INET') print 'Running on %s:%s with %s' % (args.host, args.port, database.graph) while True: connection = listener.accept() database.process(connection)
def worker(address, port, password): listener = Listener(address, authkey=password) conn = listener.accept() print 'connection accepted from', listener.last_accepted conn.send_bytes('hello') conn.close() listener.close()
def loop(self): listener = Listener(self.address, backlog=5, authkey=self.authkey) logging.warn('DB server started with %s, listening on %s:%d...', sys.executable, *self.address) self.thread.start() cmd = None try: while cmd != 'stop': try: conn = listener.accept() except KeyboardInterrupt: break except: # unauthenticated connection, for instance by a port # scanner such as the one in manage.py continue cmd_ = conn.recv() # a tuple (name, arg1, ... argN) cmd, args = cmd_[0], cmd_[1:] if cmd.startswith('@'): # slow command, run in process cmd = cmd[1:] # strip @ proc = Process( target=run_command, name=cmd, args=(cmd, args, conn)) proc.start() logging.warn('Started %s%s in process %d', cmd, args, proc.pid) else: queue.put((conn, cmd, args)) finally: listener.close() self.thread.join()
def objectLogger(params): """ Send data (dict, str, ...) into a storage """ mode = params['mode'] maxlen = params['maxlen'] file_format = params['format'] home_dir = params['storage_dir'] server = Listener((params['ip'], int(params['port']))) storage = PersistentDeque( home_dir=home_dir, maxlen=maxlen, file_format=file_format, mode=mode, ) while True: conn = server.accept() while True: try: data = conn.recv() except EOFError: break if data: storage.append(data) else: storage.sync(id_generator()) storage = PersistentDeque( home_dir=home_dir, maxlen=maxlen, file_format=file_format, mode=mode, ) conn.close()
class SimpleSynergeticServer(Process): def __init__(self, authen_key): Process.__init__(self) self.__task_queue = JoinableQueue(1) self.__return_queue = Queue(1) self.serv = Listener(('', 40000), authkey=authen_key) def run(self): print 'Server Works' copy_reg.pickle(types.MethodType, _reduce_method) #Start the synergeticProcess in Deamon Mode worker_p = SynergeticProcess(self.__task_queue, self.__return_queue) worker_p.deamon = True worker_p.start() while True: print 'wait for Client' pool_conn = self.serv.accept() print 'connection Client Accepted' while True: print 'in LOOP Simple Server' #There is no need for task_id in this version try: print 'Try to recv MSG' unpickled_msg = pool_conn.recv() print 'MSG Reseved' except Exception as e: # EOFError: print 'Fail To Receive MSG:', e break if unpickled_msg[0] == 'MODULES': self.import_mods( unpickled_msg[1] ) ret = 'MODULES-READY' else: self.__task_queue.put(unpickled_msg) ret = self.__return_queue.get() try: print 'SEND RESPONCE' try: pool_conn.send( ret ) except EOFError: print 'SENT TO POOL FAILD' print 'RESPONCE SENT ', ret except EOFError: break pool_conn.close() def import_mods(self, mods_d): for mod_name, mod_bytecode in mods_d.items(): try: fobj = open(mod_name + ".pyc", 'wb') except Exception as e: print("Synergeticprocessing.SimpleServer --> Module file error: %s" % e) else: fobj.write( mod_bytecode ) finally: fobj.close() for mod in mods_d: print 'blocking' __import__( mod ) print 'imported ', mod
def loop(self): listener = Listener(self.address, backlog=5, authkey=self.authkey) logging.warn('DB server started with %s, listening on %s:%d...', sys.executable, *self.address) try: while True: try: conn = listener.accept() except KeyboardInterrupt: break except: # unauthenticated connection, for instance by a port # scanner such as the one in manage.py continue cmd_ = conn.recv() # a tuple (name, arg1, ... argN) cmd, args = cmd_[0], cmd_[1:] logging.debug('Got ' + str(cmd_)) if cmd == 'stop': conn.send((None, None)) conn.close() break func = getattr(actions, cmd) fut = executor.submit(safely_call, func, (self.db, ) + args) def sendback(fut, conn=conn): res, etype, _mon = fut.result() if etype: logging.error(res) # send back the result and the exception class conn.send((res, etype)) conn.close() fut.add_done_callback(sendback) finally: listener.close()
def _serve(self, port, path): data = [] # print 'Server', os.getpid() address = ('localhost', port) listener = Listener(address) loop = True while loop: conn = listener.accept() command, payload = conn.recv() response_payload = None if command == 'OPEN': #print command, payload path = payload elif command == 'WRITE': #print command, payload data.append(payload) elif command == 'FETCH': #print command, payload response_payload = data elif command == 'FLUSH': #print command json.dump(data, open(path, 'w')) elif command == 'CLOSE': #print command #json.dump(data, open(path, 'w')) loop = False conn.send(('OK', response_payload)) conn.close() listener.close()
def _listenThread(self): address = ('localhost', self.port) # family is deduced to be 'AF_INET' listener = Listener(address, authkey=self.pwd) while True: conn = listener.accept() self._printDebug('MultiBus,Listner: connection accepted from' + str(listener.last_accepted)) p = Process(target=self._connThread, args=(conn,)) p.start()
def run(self): serv = Listener(self.address,authkey=self.authkey) while True: try: client = serv.accept() DispatchClientTask(client).start() except Exception as e: self.log.info("Error : %s", e, exc_info=True)
def start(self): address = ('localhost', 6000) listener = Listener(address) conn = listener.accept() while True: msg = conn.recv() self.log_id += 1 self.log_file.add_row(LogEntry([self.node_id, self.log_id, msg]))
def run(self): listener = Listener(self.address, self.family) while True: conn = listener.accept() t = threading.Thread(target=self._handle_conn, args=(conn,)) t.daemon = True t.start()
def send(msg): address = ('localhost', 6000) listener = Listener(address) conn = listener.accept() conn.send(msg) conn.close() listener.close()
def echo_server(address, authkey): serv = Listener(address, authkey=authkey) while True: try: client = serv.accept() echo_client(client) except Exception: traceback.print_exc()
class IPCServer(Thread): """ This is (sort of?) a barebones implementation of a reverse pub/sub pattern: multiple publishers can connect to this and it dispatches the events to registered subscribers. Why? Cause miss me with that zeromq shit. """ def __init__(self, address=('localhost', 60000), authkey=ipc_pass): Thread.__init__(self) self.name = 'IPCServer' self.address = address self.listener = Listener(self.address, authkey=authkey) self.daemon = True self.subscribers = {} def run(self): logging.debug(f"Started IPC server on {self.address}") while True: client = self.listener.accept() t = Thread(target=self.serve, args=(client,)) t.setDaemon(True) t.start() def attach(self, event, func): if event not in self.subscribers: self.subscribers[event] = set() self.subscribers[event].add(func) else: self.subscribers[event].add(func) def detach(self, event, func): raise NotImplemented def publish(self, topic, msg): if topic in self.subscribers: for sub in self.subscribers[topic]: #run_in_terminal(functools.partial(sub, msg)) return sub(msg) def serve(self, client): logging.debug(f"connection accepted from {self.listener.last_accepted}") while True: try: data = client.recv() except EOFError: pass topic, msg = data logging.debug(f"Got event: {topic} msg: {msg}") if topic in self.subscribers: for sub in self.subscribers[topic]: future = run_in_terminal(functools.partial(sub, msg)) future.add_done_callback(functools.partial(lambda f, c: c.send(f.result()), c=client)) else: logging.debug(f"Got event: {topic}, but there's nothing subscribed")
def run(self): listen = Listener(self._address) print 'simplepycache server start to listen at', self._address cleaner = self.Cleaner(self) cleaner.deamon = True cleaner.start() while True: conn = listen.accept() self.Worker(self, conn).start()
def client_listener(): cl = Listener(address=local_listener[0], authkey=local_listener[1]) print '.............client listener starting' print '.............accepting conexions' while True: conn = cl.accept() print '.............connection accepted from', cl.last_accepted m = conn.recv() print '.............message received from server', m
def main(): _setproctitle('structurarium.taskqueue') parser = argparse.ArgumentParser( description='Run Structurarium graph server' ) parser.add_argument( '--version', '-v', action='version', version=__version__ ) parser.add_argument('host') parser.add_argument('port', type=int) parser.add_argument('path') parser.add_argument('--authkey', '-k') parser.add_argument( '--worker', '-w', action='store', type=int, help='default is set to the number of CPU' ) args = parser.parse_args() listener = Listener((args.host, args.port), family='AF_INET') database = TaskQueue(args.path, authkey=args.authkey) print 'Running on %s:%s' % (args.host, args.port) if args.worker > 1: pool = Pool(processes=args.worker) while True: pool = Pool(processes=args.worker) connection = listener.accept() connection = cPickle.dumps(reduce_connection(connection)) database.process(connection) pool.apply_async(process, [database, connection]) else: print 'monothread' database.replay() while True: connection = listener.accept() command = loads(connection.recv()) output = database.play(command) connection.send(dumps(output)) connection.close()
def run(self): server = Listener( ('', 25000) ) logging.getLogger("thread-listener").info("Connection listener initialized.") while self.RUN: try: client = server.accept() logging.getLogger("thread-listener").debug("Client connecting...") self.echo_client(client) except Exception: traceback.print_exc()
def run( self ): # Cycling reading input from socket syslog.syslog("Listener GardenServer Started") #listener = Listener(self.address, authkey='testPwd') listener = Listener(self.serverAddress,authkey = self.authkey) while True: # Building listener syslog.syslog("waiting for connection") conn = listener.accept() print 'connection accepted from', listener.last_accepted syslog.syslog("connection accepted from ") # Receiving message msg = conn.recv() # in case of message "close" cycle exit if msg == 'close': conn.close() listener.close() return # Control if message is in proper format if string.find(msg,":") == -1: conn.close() listener.close() return elif len(msg) > 0: # Splitting message # message format: zoneId:time zoneId = string.split(msg,":")[0] duration = string.split(msg,":")[1] #create thread for the zone threadZone = ZoneThread() self.threadList.append(threadZone) try: #set parameters in thread Zone threadZone.setParameters(duration,zoneId,1) syslog.syslog("opening zone %s for %s @%s" % (zoneId,duration,time.strftime("%Y-%m-%d %H:%M:%S"))) if duration == - 1: threadZone.close() else: threadZone.start() except RuntimeError: syslog.syslog("Error opening zone %s" % (zone )) listener.close() return
class Server(object): def __init__(self, refmon, address, port, key): self.logger = util.log.getLogger('RefMon_Server') self.logger.info('server: start') self.refmon = refmon #self.listener = Listener((address, port), authkey=str(key), backlog=100) self.listener = Listener((address, port), backlog=128000) self.logger.debug('FLANCPORT' + str(port)) def start(self): self.receive = True self.receiver = Thread(target=self.receiver) self.newReceiver = Thread(target=self.newReceiver) self.receiver.start() self.newReceiver.start() def newReceiver(self): r = redis.StrictRedis(host='localhost', port=6379, db=0) self.logger.debug('FLANC newReceiver created redis object' + str(r)) tempIncVar=1 while self.receive: (qname, flow) = r.blpop('flowqueue') self.logger.info('REDISSTAT nr of flows in redis: ' + str(r.llen('flowqueue'))) self.logger.info('REDIS Consuming nr: ' + str(tempIncVar)) self.refmon.process_flow_mods(json.loads(flow)) tempIncVar = tempIncVar + 1 self.logger.debug('RECVSTOP newReceiver is out of receiveloop, thread will end now') ''' receiver ''' def receiver(self): while self.receive: conn = self.listener.accept() self.logger.info('server: accepted connection from ' + str(self.listener.last_accepted)) msg = None while msg is None: try: msg = conn.recv() self.logger.info('SUCCESSFUL DEBUGGING: ' + str(msg)) except Exception as e: self.logger.info('RECEIVING DEBUGGING: ' + str(e.message)) self.logger.info('RECEIVING DEBUGGING: ' + str(e.__doc__)) pass self.logger.info('server: received message') self.refmon.process_flow_mods(json.loads(msg)) conn.close() self.logger.info('server: closed connection') self.logger.debug('RECVSTOP receiver is out of loop, thread will end now') def stop(self): self.logger.debug("STOPFLANC setting self.receive to false, stopping listener") self.receive = False self.receiver.join(1)
def listener(): address = ('10.33.41.112', 6666) listener = Listener(address, backlog=100, authkey='hellokey') while True: conn = listener.accept() #print 'connection accepted from', listener.last_accepted try: conn.send({'1':2, '2':'abc'}) except Exception, e: print e finally:
def start_serve(self): """start the server""" while True: listener = Listener(self.address) conn = listener.accept() process = ClientModuleConnect(listener.last_accepted, conn) process.start() listener.close()
def listen_code(self): listener = Listener(address=self._address, family='AF_UNIX') while not self.quit.isSet(): self.code_channel = listener.accept() while not self.quit.isSet(): try: msg = self.code_channel.recv() self.code_lines.append(msg) except EOFError: # client exited but it may be started again continue
def run(self): """ Main angel loop """ # Open angel socket authkey = self.settings.angel_authkey.encode('utf-8') listener = Listener( str(self.settings.get_path('angel_socket')), self.settings.angel_family, authkey=authkey, ) multiprocessing.current_process().authkey = authkey # Give the listener a fileno so we can use it directly with select listener.fileno = listener._listener._socket.fileno # Main angel loop socket_to_process = {} process_to_socket = {} old_process = None start_delay = 0 self.running = True while self.running: # We don't have an active process, so there shouldn't be others # If there are, terminate them for process, process_socket in list(process_to_socket.items()): if process.poll(): process.terminate() process_socket.close() del process_to_socket[process] del socket_to_process[process_socket] # There is nothing valid in the store # Either this is the first time it's running, # or it failed, so didn't get a chance to serialise anything store = None # Start script, with increasing delay if it last start failed if start_delay: time.sleep(start_delay) if start_delay < START_DELAY_MAX: start_delay *= 2 else: start_delay = 1 self.active_process = self.start_process() # Loop while the script is running while self.active_process.poll() is None: # Check old processes for dead for process in process_to_socket.keys(): if process == self.active_process: # Already checked active process continue if process.poll() is not None: self.log.angel('Process %s has died' % process.pid) process_to_socket[process].close() del process_to_socket[process] del socket_to_process[process_socket] # Check sockets for activity read_sockets = [] send_sockets = [] try: read_sockets, send_sockets = select.select( [listener] + list(socket_to_process.keys()), [], [], 1, )[0:2] except select.error as e: self.log.angel('Angel select error: %s' % e) except socket.error as e: self.log.angel('Angel socket error: %s' % e) # Check listener socket for new connections if listener in read_sockets: # Only leave process sockets on read_sockets del read_sockets[read_sockets.index(listener)] # Accept the connection process_socket = listener.accept() # Check this makes sense if self.active_process in process_to_socket: # We already have a socket for the active process. # This shouldn't happen - only explanation is this is # an intruder. Ignore it. self.log.angel( 'New connection for existing process %s ignored' % self.active_process.pid ) process_socket.close() # Store against the current process self.log.angel( 'Established connection to process %s' % self.active_process.pid ) socket_to_process[process_socket] = self.active_process process_to_socket[self.active_process] = process_socket # Check process sockets who want to say something for process_socket in read_sockets: process = socket_to_process[process_socket] try: raw = process_socket.recv() except Exception as e: # We were told there would be something to read process_socket.close() try: process.terminate() except OSError: # Already terminated pass del process_to_socket[process] del socket_to_process[process_socket] continue # Get command and data if len(raw) == 2: cmd, data = raw else: self.log.angel( 'Invalid data received from process %s' % process.pid ) cmd, data = None, None # Process cmd if cmd == CMD_GET_SERVICE: # Process has started and wants serialised data process_socket.send((CMD_OK, store)) if store is not None: self.log.angel( 'Sent service to process %s' % process.pid ) # Empty store - can only be deserialised once store = None elif cmd == CMD_SET_SERVICE: # Process is sending us serialised service data store = data # Process is now waiting for the OK before it stops. # Don't send it yet - only stop once we get CMD_STARTED # from the new process. old_process = self.active_process self.log.angel( 'Received service from process %s' % process.pid ) # Start new process self.active_process = self.start_process() elif cmd == CMD_STARTED: # New process has started; cmd sent after PostStart process_socket.send((CMD_OK, None)) # It's not going to fall over immediately, reset delay start_delay = 0 # Tell old process it's ok to die now if old_process: if old_process in process_to_socket: process_socket = process_to_socket[old_process] process_socket.send((CMD_OK, None)) self.log.angel( 'Process %s active, terminating process %s' % ( process.pid, old_process.pid ) ) else: self.log.angel('Process %s active' % process.pid) elif cmd == CMD_POLL: process_socket.send((CMD_OK, None)) elif cmd == CMD_LOG: process_socket.send((CMD_OK, None)) self.log._write(data) else: # Something has gone wrong - kill them self.log.angel( 'Command not recognised, terminating process %s' % process.pid ) process_socket.close() if process.poll() is None: process.terminate() del process_to_socket[process] del socket_to_process[process_socket] continue # Active process has died if self.running: # It died unexpectedly self.log.angel( 'Active process %s died unexpectedly' % self.active_process.pid ) else: # We're shutting down self.log.angel( 'Active process %s terminating' % self.active_process.pid ) if self.active_process in process_to_socket: # Clean up process_socket = process_to_socket[self.active_process] process_socket.close() del process_to_socket[self.active_process] del socket_to_process[process_socket]
class Publisher: """Publisher Class responsible for setting up nodes and their connection. Employs basic publish/subscribe model. Data may be send via any of TCP, UDS or named Windows Pipe. """ def __init__(self, address, max_q_size=None, timeout=None): """Initialize Instance. :param sock_name: :param max_q_size: :param timeout: """ self._address = address self._subscribers = set() self._subscriber_nodes = {} self._running = Event() self.connection = Listener(address) self._node_factory = lambda x: Distributor(x, max_q_size, timeout) def attach(self, subscriber): """Attach a subscriber to the publisher. :param subscriber: string, UDS Path| TCP Address Tuple | Named Pipe :return: """ self._subscribers.add(subscriber) self._subscriber_nodes[subscriber] = self._node_factory(subscriber) self._subscriber_nodes[subscriber].start() def detach(self, subscriber): """Detaches the given subscriber from the publisher. :param subscriber: string, UDS Path| TCP Address Tuple | Named Pipe :return: """ self._subscribers.remove(subscriber) removed_sub = self._subscriber_nodes.pop(subscriber) if removed_sub.is_alive(): removed_sub.join() def publish(self, data): """Publish the given data to all current subscribers. :param data: :return: """ for subscriber in self._subscribers: if self._subscriber_nodes[subscriber].is_alive(): self._subscriber_nodes[subscriber].send(data) else: self.detach(subscriber) def stop(self): """Sends shutdown sentinel signal to main loop. :return: """ try: sentinel_conn = Client(self._address) sentinel_conn.send('$$$') except (EOFError, ConnectionResetError, ConnectionAbortedError): pass def _shut_down(self): self._running.clear() for sub in self._subscribers: self.detach(sub) os.remove(self._address) def handle_conns(self): while self._running.is_set(): try: client = self.connection.accept() sub = client.recv() if sub == '$$$': self._shut_down() else: self.attach(sub) client.send('ok') client.close() except EOFError: continue except Exception as e: raise
class Distributor(Thread): """Base Class providing a AF_INET, AF_UNIX or AF_PIPE connection to its data queue. It offers put() and get() method wrappers, and therefore behaves like a Queue as well as a Thread. Data from the internal queue is automatically fed to the connecting client. """ def __init__(self, address, max_q_size=None, timeout=None, *thread_args, **thread_kwargs): """Initialize class. :param sock_name: UDS, TCP socket or pipe name :param max_q_size: maximum queue size for self.q, default infinite """ self.address = address self.connector = Listener(address) max_q_size = max_q_size if max_q_size else 0 self.q = Queue(maxsize=max_q_size) self._running = Event() self.connection_timer = Timer(timeout, self.connection_timed_out) super(Distributor, self).__init__(*thread_args, **thread_kwargs) def connection_timed_out(self): """Closes the Listener and shutsdown Distributor if no Client connected. We do this by temporarily connecting to ourselves using a Client(), and instantly closing the connection. :return: """ self._running.clear() sentinel_conn = Client(self.address) sentinel_conn.close() self.connector.close() self.join() os.remove(self.address) def _start_connection_timer(self): self.connection_timer.start() def start(self): self._running.set() super(Distributor, self).start() def join(self, timeout=None): self._running.clear() super(Distributor, self).join(timeout=timeout) def run(self): while self._running.is_set(): self._start_connection_timer() try: client = self.connector.accept() self.connection_timer.cancel() self.feed_data(client) except (TimeoutError, socket.timeout, ConnectionError): continue except Exception as e: raise def feed_data(self, client): try: while self._running.is_set(): try: client.send(self.q.get()) except Empty: continue except EOFError: return def send(self, data, block=True, timeout=None): self.put(data, block, timeout) def put(self, item, block=True, timeout=None): """put() wrapper around self.q.put() :param item: :param block: :param timeout: :return: """ self.q.put(item, block, timeout) def get(self, block=True, timeout=None): """get() wrapper around self.q.get() :param block: :param timeout: :return: """ self.q.get(block, timeout)
class InternalDeferredPlugin(object): def __init__(self, config): self.ida_path = config.getoption('--ida') self.ida_file = config.getoption('--ida-file') self.record_file = config.getoption('--ida-record') self.keep_ida_running = config.getoption('--ida-keep') self.config = config self.session = None self.listener = Listener() self.conn = None self.logfile = tempfile.NamedTemporaryFile(delete=False) self.proc = None self.stop = False def ida_start(self): internal_script = os.path.join(os.path.dirname(__file__), "main_idaworker.py") idapro_internal_dir = os.path.join(os.path.dirname(__file__), "idapro_internal") record_module_template = os.path.join(idapro_internal_dir, "init.py.tmpl") ida_python_init = os.path.join(os.path.dirname(self.ida_path), "python", "init.py") try: if self.record_file: self.install_record_module(idapro_internal_dir, record_module_template, ida_python_init) script_args = '{}'.format(self.listener.address) args = [ self.ida_path, # autonomous mode. IDA will not display dialog boxes. # Designed to be used together with -S switch. "-A", "-S\"{}\" {}".format(internal_script, script_args), "-L{}".format(self.logfile.name), # Load user-provided or start with an empty database self.ida_file if self.ida_file else "-t" ] log.debug("worker execution arguments: %s", args) self.proc = subprocess.Popen(args=args) # accept a single connection self.conn = self.listener.accept() self.listener.close() self.listener = None finally: if self.record_file: self.uninstall_record_module(record_module_template, ida_python_init) def ida_finish(self, interrupted): self.stop = True if interrupted: log.warning( "Abrupt termination of external test session. worker " "log: %s", self.logfile.read()) if not self.proc: return # calling poll to poll execution status and returncode self.proc.poll() if self.proc.returncode is not None: return if self.keep_ida_running: log.info("Avoiding forcefully killing ida because requested to " "keep it running") return log.info("Stopping...") self.proc.kill() def install_record_module(self, idapro_internal_dir, record_module_template, ida_python_init): log.info("Installing record module") base_paths = {os.path.dirname(self.ida_path)} root_dir = self.config.rootdir.strpath for p in self.config.getoption('file_or_dir'): base_paths.add(os.path.abspath(os.path.join(root_dir, p)) + "/") template_params = { 'idapro_internal_dir': idapro_internal_dir, 'base_paths': base_paths } with open(record_module_template, 'r') as fh: lines = [line.format(**template_params) for line in fh.readlines()] with open(ida_python_init, 'r') as fh: lines += fh.readlines() with open(ida_python_init, 'w') as fh: fh.writelines(lines) @staticmethod def uninstall_record_module(record_module_template, ida_python_init): log.info("Uninstalling record module") with open(record_module_template, 'r') as fh: lastline = fh.readlines()[-1] lines = [] with open(ida_python_init, 'r') as fh: for line in fh.readlines(): lines = [] if line == lastline else (lines + [line]) with open(ida_python_init, 'w') as fh: fh.writelines(lines) def command_ping(self): self.send('ping') self.recv('pong') def command_dependencies(self): plugins = [] if (hasattr(self.config.option, 'cov_source') and self.config.option.cov_source): plugins.append("pytest_cov") self.send('dependencies', 'check', *plugins) if self.recv('dependencies') == ('ready', ): return self.send('dependencies', 'install', *plugins) self.recv('dependencies', 'ready') def command_autoanalysis_wait(self): self.send('autoanalysis', 'wait') self.recv('autoanalysis', 'done') def command_configure(self, config): option_dict = copy.deepcopy(vars(config.option)) # block interfering plugins option_dict['plugins'].append("no:cacheprovider") option_dict['plugins'].append("no:pytest-qt") option_dict['plugins'].append("no:xdist") option_dict['plugins'].append("no:xvfb") option_dict['usepdb'] = False # cleanup our own plugin configuration option_dict["plugins"].append("no:idapro") del option_dict['ida'] del option_dict['ida_file'] del option_dict['ida_record'] del option_dict['ida_replay'] if platform.system() == "Windows": # remove capturing, this doesn't properly work in windows option_dict["plugins"].append("no:terminal") option_dict["capture"] = "sys" self.send('configure', config.args, option_dict) self.recv('configure', 'done') def command_cmdline_main(self): self.send('cmdline_main') self.recv('cmdline_main', 'start') def command_session_start(self): self.recv('session', 'start') # we do not start the session twice # self.config.hook.pytest_sessionstart(session=self.session) def command_report_header(self): startdir, = self.recv('report', 'header') self.config.hook.pytest_report_header(config=self.config, startdir=startdir) def command_collect(self): self.recv('collection', 'start') self.config.hook.pytest_collectstart() while True: r = self.recv('collection') if r[0] == 'report': report = self.deserialize_report("collect", r[1]) self.config.hook.pytest_collectreport(report=report) elif r[0] == 'finish': collected_tests = r[1] self.session.testscollected = len(collected_tests) self.config.hook.pytest_collection_finish(session=self.session) break elif r[0] == 'modifyitems': self.config.hook.pytest_collection_modifyitems( session=self.session, config=self.config, items=r[1]) elif r[0] == 'deselected': self.config.hook.pytest_deselected(items=r[1]) else: raise RuntimeError("Invalid collect response received: " "{}".format(r)) def command_runtest(self): while True: r = self.recv('runtest') if r[0] == 'logstart': self.config.hook.pytest_runtest_logstart(nodeid=r[1], location=r[2]) elif r[0] == 'logreport': report = self.deserialize_report("test", r[1]) self.config.hook.pytest_runtest_logreport(report=report) elif r[0] == 'logfinish': # the pytest_runtest_logfinish hook was introduced in pytest3.4 if hasattr(self.config.hook, 'pytest_runtest_logfinish'): self.config.hook.pytest_runtest_logfinish(nodeid=r[1], location=r[2]) elif r[0] == 'finish': break else: raise RuntimeError("Invalid runtest response received: " "{}".format(r)) def command_report_terminalsummary(self): exitstatus = self.recv('report', 'terminalsummary') tr = self.config.pluginmanager.get_plugin('terminalreporter') self.config.hook.pytest_terminal_summary(terminalreporter=tr, exitstatus=exitstatus) def command_quit(self): self.send('quit', not self.keep_ida_running) self.recv('quitting') def command_save_records(self): self.send('save_records', self.record_file) self.recv('save_records', 'done') def send(self, *s): log.debug("Sending: %s", s) return self.conn.send(s) def recv(self, *args): try: while not self.conn.poll(1): if self.stop: raise KeyboardInterrupt r = self.conn.recv() log.debug("Received: %s", r) except Exception: log.critical("Exception during receive, worker output: %s", self.logfile.read()) raise if args and r[:len(args)] != args: raise RuntimeError("Invalid response recieved; while expecting " "'{}' got '{}'".format(args, r)) return r[len(args):] def deserialize_report(self, reporttype, report): from _pytest.runner import TestReport, CollectReport from pytest import Item if 'result' in report: newresult = [] for item in report['result']: item_obj = Item(item['name'], config=self.config, session=self.session) newresult.append(item_obj) report['result'] = newresult if reporttype == "test": return TestReport(**report) elif reporttype == "collect": return CollectReport(**report) else: raise RuntimeError("Invalid report type: {}".format(reporttype)) def pytest_runtestloop(self, session): self.session = session if self.config.pluginmanager.has_plugin('_cov'): from .idapro_internal.cov import CovReadOnlyController cov_plugin = self.config.pluginmanager.get_plugin('_cov') CovReadOnlyController.silence(cov_plugin.cov_controller) try: self.ida_start() self.command_ping() self.command_dependencies() self.command_autoanalysis_wait() self.command_configure(self.config) self.command_cmdline_main() self.command_session_start() self.command_report_header() self.command_collect() response = self.recv() if response == ('runtest', 'start'): self.command_runtest() exitstatus = self.recv('session', 'finish') elif response[:2] == ('session', 'finish'): exitstatus = response[2] else: raise RuntimeError("Unexpected response: {}".format(response)) # TODO: The same exit status will be derived by pytest. might be # useful to make sure they match del exitstatus self.command_report_terminalsummary() self.recv('cmdline_main', 'finish') if self.record_file: self.command_save_records() self.command_quit() except Exception: log.exception("Caught exception during main test loop") self.ida_finish(True) raise return True def pytest_sessionfinish(self, exitstatus): self.ida_finish(exitstatus == 2) # EXIT_ITERRUPTED @staticmethod def pytest_collection(): # prohibit collection of test items in master process. test collection # should be done by a worker with access to IDA modules return True
received = 0 lista, total = cutter(lsize, chunksize) thesize = len(lista) #print(lista) #print(total) didstart = False ok = True #the ok variable is the variable that allows printing, i put it here to avoid excessive printing while received < thesize: if ok is True and toprint: #some data reports along the processing print("----------") print(received) print("----------") try: if ok is True and toprint: print("Waiting connections") c = s.accept() # Establish connection with slave. used.add(c.address) print(c.address) status = c.recv().decode( 'utf-8') #gets the status message and then decode if not didstart: start = time.time() didstart = True if ok is True and toprint: print(str(c) + " is " + str(status.split("|")[0])) if status == "ready" and conta < thesize: # checks if the slave is ready for a task and if there is tasks avaliable c.send(bytes(str(lista[conta]), "utf-8")) # sends the next task conta += 1 ok = True elif str(
class MainManager(QObject): instance = None # Signals nodes_changed = pyqtSignal(str, tuple, int) activenode_changed = pyqtSignal(tuple, int) @classmethod def sender(cls): return cls.instance.sender_id @classmethod def actnode(cls): return cls.instance.activenode def __init__(self): super(MainManager, self).__init__() print 'Initializing multi-process simulation' MainManager.instance = self self.scenarios = [] self.connections = [] self.localnodes = [] self.hosts = dict() self.max_nnodes = min(cpu_count(), max_nnodes) self.activenode = 0 self.sender_id = None self.stopping = False self.listener = Listener(('localhost', 6000), authkey='bluesky') def receiveFromNodes(self): # Only look for incoming data if we're not quitting if self.stopping: return # First look for new connections r, w, e = select.select((self.listener, ), (), (), 0) if self.listener in r: conn = self.listener.accept() address = self.listener.last_accepted[0] if address in self.hosts: nodeid = self.hosts[address] else: nodeid = (len(self.hosts), 0) # Store host number and its number of nodes self.hosts[address] = (nodeid[0], nodeid[1] + 1) # Send the node information about its nodeid connidx = len(self.connections) conn.send((SetNodeIdType, nodeid)) self.connections.append([conn, nodeid, 0]) self.nodes_changed.emit(address, nodeid, connidx) self.setActiveNode(connidx) # Then process any data in the active connections for connidx in range(len(self.connections)): conn = self.connections[connidx] if conn[0] is None or conn[0].closed: continue # Check for incoming events with poll while conn[0].poll(): # Receive events that are waiting in the conn try: (eventtype, event) = conn[0].recv() except: continue # Sender id is connection index and node id self.sender_id = (connidx, conn[1]) if eventtype == AddNodeType: # This event only consists of an int: the number of nodes to add for i in range(event): self.addNode() continue # Data over connections is pickled/unpickled, this causes problems with # inherited classes. Solution is to call the ancestor's init QEvent.__init__(event, eventtype) # First check if this event is meant for the manager if event.type() == SimStateEventType: # Save the state together with the connection object conn[2] = event.state if event.state == event.end: # Quit the main loop. Afterwards, manager will also quit qapp.instance().quit() elif event.state == event.init or event.state == event.hold: if len(self.scenarios) > 0: self.sendScenario(conn) elif event.type() == BatchEventType: self.scenarios = [ scen for scen in split_scenarios( event.scentime, event.scencmd) ] # Check if the batch list contains scenarios if len(self.scenarios) == 0: qapp.sendEvent( qapp.instance(), StackTextEvent( disptext='No scenarios defined in batch file!') ) else: qapp.sendEvent( qapp.instance(), StackTextEvent( disptext='Found %d scenarios in batch' % len(self.scenarios))) # Available nodes (nodes that are in init or hold mode): av_nodes = [ n for n in range(len(self.connections)) if self.connections[n][2] in [0, 2] ] for i in range(min(len(av_nodes), len(self.scenarios))): self.sendScenario(self.connections[i]) # If there are still scenarios left, determine and start the required number of local nodes reqd_nnodes = min( len(self.scenarios), max(0, self.max_nnodes - len(self.localnodes))) for n in range(reqd_nnodes): self.addNode() else: # The event is meant for the gui qapp.sendEvent(qapp.instance(), event) # To avoid giving wrong information with sender() when it is called outside # of this function, set sender_id to None self.sender_id = None def addNode(self): if len(self.connections) > 0: self.connections[self.activenode][0].send( (SetActiveNodeType, False)) p = Popen([sys.executable, 'BlueSky_qtgl.py', '--node']) self.localnodes.append(p) def sendScenario(self, conn): # Send a new scenario to the target sim process scen = self.scenarios[0] qapp.sendEvent( qapp.instance(), StackTextEvent(disptext='Starting scenario ' + scen[0] + ' on node (%d, %d)' % conn[1])) conn[0].send((BatchEventType, BatchEvent(scen[1], scen[2]))) # Delete the scenario from the list del self.scenarios[0] def setActiveNode(self, connidx): if connidx < len(self.connections): self.activenode_changed.emit(self.connections[connidx][1], connidx) if not connidx == self.activenode: self.connections[self.activenode][0].send( (SetActiveNodeType, False)) self.activenode = connidx self.connections[self.activenode][0].send( (SetActiveNodeType, True)) def start(self): timer = QTimer(self) timer.timeout.connect(self.receiveFromNodes) timer.start(20) # Start the first simulation node on start self.addNode() def stop(self): print 'Stopping simulation processes...' self.stopping = True # Tell each process to quit quitevent = (SimQuitEventType, SimQuitEvent()) print 'Stopping nodes:' for n in range(len(self.connections)): self.connections[n][0].send(quitevent) # Wait for all nodes to finish for n in self.localnodes: n.wait() for n in range(len(self.connections)): self.connections[n][0].close() print 'Done.' def event(self, event): # Only send custom events to the active node if event.type() >= 1000: self.connections[self.activenode][0].send( (int(event.type()), event)) return True
class queue_server: def __init__(self, queue_worker, max_queue_length, uri_root): self._listener = Listener(socket_name, 'AF_UNIX') self._queue_worker = queue_worker self._max_queue_length = max_queue_length self._uri_root = uri_root def _handle_enqueue(self, msg, conn): # Refuse new jobs if the specified max. queue length is reached. # This can prevent "submission-bomb" DoS attacks. if(self._queue_worker.get_num_jobs() >= self._max_queue_length): conn.send([(False, "Request to enqueue has been denied. Queue has reached maximum allowed length.")]) return # Refuse new jobs if the queue has shutdown if not self._queue_worker.is_running(): conn.send([(False, "Queue has shutdown.")]) return try: jdata = job_data(msg) job_id = self._queue_worker.new_job(jdata) conn.send([(True, "Job is enqueued."), job_id]) except Exception as e: conn.send([(False, "Could not enqueue job: "+str(e))]) def listen(self): print("********* Listening for instructions ***********") while True: try: conn = self._listener.accept() msg = conn.recv() # Only log the the first three fields since the fourth is the environment, # which would just clutter the log. print("Received message:",msg[:3]) if(msg[0] == 'enqueue'): self._handle_enqueue(msg[1],conn) elif(msg[0] == 'cancel'): job_id = msg[1] result = self._queue_worker.cancel_job(job_id) if not result: conn.send([result]) else: conn.send([result]) elif(msg[0] == 'shutdown'): conn.send([(True, "Queue will shutdown as soon as all running jobs have completed.")]) self._queue_worker.shutdown() elif(msg[0] == 'uri_query'): uri = msg[1] try: permissions = uri_permissions.CLIENT access = uri_accessor(uri, self._uri_root, permissions) result = access.read(permissions) conn.send([(True, "URI has been read."), result]) except uri_exception_read_only: conn.send([(False, "URI is read-only.")]) except uri_exception_permission_denied: conn.send([(False, "Access to URI has been denied.")]) except uri_exception_not_found: conn.send([(False, "Object at specified URI does not exist.")]) except uri_exception_no_such_attribute: conn.send([(False, "Attribute specified by URI does not exist.")]) except Exception as e: conn.send([(False, str(e))]) raise else: print("Warning: Message is unknown, cannot be handled:",msg) conn.send([(False, "Invalid message")]) conn.close() except Exception as e: print("Warning: Exception occured during handling of message:", e) #raise self._listener.close()
class ServerEngine(EngineBase): DictationContainer = None @staticmethod def communicate(data): conn = Client(ADDRESS) conn.send(data) received = conn.recv() return received @staticmethod def get_server_engine(): try: return ServerEngine.communicate(Action.GET_ENGINE) except socket.error: return ServerEngineType.NONE def __init__(self, type): EngineBase.__init__(self) self.__type = type if type == ServerEngineType.NATLINK: ServerEngine.DictationContainer = NatlinkDictationContainer else: ServerEngine.DictationContainer = Sapi5DictationContainer self._queue = [] self.listener = Listener(ADDRESS) self.listener._listener._socket.settimeout(TIMEOUT) self._running = True threading.Thread(target=self.loop).start() def loop(self): while self._running: try: conn = self.listener.accept() except socket.timeout: continue except socket.error: break else: self.handle_connection(conn) self.listener.close() def stop(self): self._running = False def handle_connection(self, conn): msg = conn.recv() action_answer = self.handle_action(msg) if action_answer is not None: conn.send(action_answer) else: data_answer = self.handle_data(msg) if data_answer: conn.send(data_answer) def handle_action(self, message): if message == Action.FETCH_WORK: if len(self._queue) == 0: return Action.ACK else: value = self._queue[0] self._queue.remove(value) return value if message == Action.GET_ENGINE: return self.__type return None def handle_data(self, message): action, data = message if action == Action.RULE_PROCESS_RECOGNITION: id, node = data remote.process_recognition(id, node) return Action.ACK if action == Action.WRITE_OUTPUT: sys.stdout.write(data) return Action.ACK if action == Action.WRITE_ERROR: sys.stderr.write(data) return Action.ACK return None def connect(self): self._queue.append(WorkType.CONNECT) def disconnect(self): self._queue.append(WorkType.DISCONNECT) def _load_grammar(self, grammar): self._queue.append((WorkType.LOAD_GRAMMAR, remote.create_remote_grammar(grammar))) def _unload_grammar(self, grammar, wrapper): self._queue.append((WorkType.UNLOAD_GRAMMAR, remote.get_remote_grammar_id(grammar))) def on_grammar_enable(self, grammar): self._queue.append((WorkType.ENABLE_GRAMMAR, remote.get_remote_grammar_id(grammar))) def on_grammar_disable(self, grammar): self._queue.append((WorkType.DISABLE_GRAMMAR, remote.get_remote_grammar_id(grammar))) def update_list(self, list, grammar): if isinstance(list, __builtin__.list): data = __builtin__.list(list) else: data = dict(list) self._queue.append((WorkType.UPDATE_LIST, remote.get_remote_grammar_id(grammar), remote.get_remote_list_id(list), data)) def mimic(self, words): raise NotImplementedError() def speak(self, text): raise NotImplementedError() def _get_language(self): return "en"
def __init_listener(self): address = os.path.join(self.plc.tmp_path, 'plc_socket') # Ensure that socket does not exist try: os.unlink(address) except OSError: if os.path.exists(address): logger.exception('Could not remove socket file.') # Create listener listener = Listener(address) logger.debug('Listener ready.') while True: conn = listener.accept() msg = conn.recv() if isinstance(msg, StartMessage): conn.send(self.plc.start()) elif isinstance(msg, StopMessage): conn.send(self.plc.stop()) elif isinstance(msg, ShowTagsMessage): conn.send(ShowTagsResponseMessage(self.plc.vars)) elif isinstance(msg, GetTagMessage): try: res = GetTagResponseMessage( self.plc.get_var_value(msg.name)) except UnknownPlcTagException as e: res = e conn.send(res) elif isinstance(msg, SetTagMessage): try: self.plc.set_var_value(msg.name, msg.value) res = SetTagResponseMessage() except UnknownPlcTagException as e: res = e conn.send(res) elif isinstance(msg, GetTagsMessage): tags = map( lambda tag: { 'name': tag['name'], 'value': self.plc.get_var_value(tag['name']) }, self.plc.vars) conn.send(GetTagsResponseMessage(tags)) elif isinstance(msg, MonitorMessage): # Wait until build is finished while not self.plc.build_finished: sleep(1) var_names = [] for x in msg.tag_names: if any(d['name'].upper() == x.upper() for d in self.plc.vars): var_names.append(x.upper()) else: logger.error( 'Found invalid var %s when trying to monitor PLC variable.', x) self.plc.conn_watcher.append({ 'conn': conn, 'var_names': var_names }) elif isinstance(msg, GetAllTagNamesMessage): tag_names = map(lambda t: t['name'], self.plc.vars) logger.debug(tag_names) conn.send(GetAllTagNamesResponseMessage(tag_names)) elif isinstance(msg, TerminateMessage): # Notify all clients that monitor that PLC is terminating. logger.debug("PLC is terminating...") for i in self.plc.conn_watcher: i['conn'].send(TerminateMessage()) self.plc.stop() self.plc.plc_thread.join() logger.debug("PLC thread joined.") conn.close() break elif isinstance(msg, CloseMessage): conn.close() break listener.close() logger.debug("Listener closed.")
def cmds(threadname): # Uncomment the following line if THIS THREAD # will need to modify the "run" variable. DO NOT # need to uncomment it to just READ it... global run # Start the command-listener (socket on localhost) address = ('127.0.0.1', ROBOT_CMD_PORT) listener = Listener(address, authkey=ROBOT_SECRET_KEY) while run: try: conn = listener.accept() #print "RECEIVER accepted connection!" while conn is not None: try: temp = conn.recv() print("RECEIVED: %s" % temp) for cmd in temp: # Write cmd back to response-file with open(RSP_FILE, "a") as f2: f2.write(cmd) f2.write("\n") f2.close() print("DEBUG: cmd = [%s]" % cmd) if CMD_START in cmd and run == 1: # Tell robot to go! run = 2 if CMD_STOP in cmd: # Tell all threads to stop! run = 0 # Close the connection from the current user conn.close() conn = None if CMD_CAMERA_LEFT in cmd: # Turn camera servo LEFT servo_left() if CMD_CAMERA_FORWARD in cmd: # Turn camera servo FORWARD servo_forward() if CMD_CAMERA_RIGHT in cmd: # Turn camera servo RIGHT servo_right() if CMD_RESET_LIDAR in cmd: # Reset LIDAR reset_lidar() except EOFError: # Close the connection from the (now-non-existent!) user conn.close() conn = None time.sleep(0.25) except IOError: # File doesn't exist True except IOError: # No instructions in file True run = 0 # Close this thread's socket/listener listener.close() print("\n\t\t***Thread %s exiting." % threadname)
pass return output if __name__ == "__main__": print 'esperando conexion' # listener= Listener(address=('147.96.18.215', 6000),authkey='secret') listener = Listener(address=('localhost', 6000), authkey='secret') manager = Manager() tablero = manager.list([8] * 16) control = Condition() jugando = manager.list([0, 0]) while True: conexion = listener.accept() print 'Se ha conectado', listener.last_accepted equipo = randint(0, 1) conexion.send(equipo) p = Process(target=jugar, args=(conexion, listener.last_accepted, tablero, control, jugando, equipo)) p.start() # p.join() listener.close() """ def editar_tablero(mensaje, tablero, last_accepted): output = 'Fail' # esto ahora es un 1 print listener.last_accepted, 'está intentando editar el tablero' try:
from functions import rc_time import RPi.GPIO as GPIO import time from multiprocessing.connection import Listener address = ('localhost', 6000) listener = Listener(address, authkey='ldr') conn = listener.accept() print 'connection accepted from' , listener.last_accepted try: while True: msg = conn.recv() if msg[0] == 'ldr': print 'ldr reading: ' , msg[1] #post request to the server elif msg[0] == 'ds': print 'ds reading: ', msg[1] #post request to the server elif msg=='patch found': print msg #post request to the server except KeyboardInterrupt: pass
#!/usr/bin/python import _thread from multiprocessing.connection import Listener import threading address = ('localhost', 6000) # family is deduced to be 'AF_INET' listener = Listener(address, authkey=b'secret password') address2 = ("localhost", 7000) listener2 = Listener(address2, authkey=b'password') conn = listener.accept() conn2 = listener2.accept() print('connection accepted from listener', listener.last_accepted) print('connection accepted from listener2', listener2.last_accepted) def acceptFirst(): while True: msg = conn.recv() msg = str(msg) print(msg) def acceptSecond(): while True: msg = conn2.recv() msg = str(msg) print(msg)
class foqusListener(threading.Thread): """ A multiprocessing listener to allow FOQUS to be controlled over a socket connection. The main purpose for this adaptive sampling. """ def __init__(self, dat, host='localhost', port=56001): threading.Thread.__init__(self) self.daemon = True self.inputNames = [] self.outputNames = [] self.resStoreSet = 'listener' self.runid = 0 self.dat = dat self.failValue = -111111 self.samples = [] self.gt = None self.scaled = False # Create a listener self.address = (host, port) self.listener = Listener(self.address) def setInputs(self, l): self.inputNames = l def setOutputs(self, l): self.outputNames = l def run(self): """Called by Thread when you run start() method""" quitListening = False # create an input dictionary structure to load values from inpDict = self.dat.flowsheet.saveValues()['input'] # Enter loop waiting for requests from client while True: #Wait for a connection to be made conn = self.listener.accept() while True: msg = conn.recv() if not (msg and isinstance(msg, list) and msg[0]): # ignore improperly formatted messages continue if msg[0] == "close": # close the connection conn.close() break elif msg[0] == "quit": # close the connection and don't wait for another quitListening = True conn.close() break elif msg[0] == "clear": # clear the list of samples self.samples = [] elif msg[0] == "run": # Start up a thread to run samples # Run samples either locally or through Turbine if self.dat.foqusSettings.runFlowsheetMethod == 0: #run local self.gt = self.dat.flowsheet.runListAsThread( self.samples) else: #run in turbine self.gt = self.dat.flowsheet.runListAsThread( self.samples, useTurbine=True) conn.send(['run', len(self.samples)]) elif msg[0] == "submit": # put a run on the input queue varVals = msg[1] #List of variable values sampInput = copy.deepcopy(inpDict) vals = self.dat.flowsheet.input.unflatten( self.inputNames, varVals, unScale=self.scaled) for nkey in vals: for vkey in vals[nkey]: sampInput[nkey][vkey] = vals[nkey][vkey] self.samples.append(sampInput) runIndex = len(self.samples) - 1 conn.send(['submitted', runIndex]) elif msg[0] == 'status': # send run status conn.send(['status', self.gt.status]) elif msg[0] == 'result': #Store results in FOQUS and send them to client also self.gt.join() ret = [] stat = [] for res in self.gt.res: self.dat.flowsheet.results.addFromSavedValues( self.resStoreSet, 'res_{0}'.format(self.runid), None, res) self.runid += 1 stat.append(res['graphError']) r = [] for vn in self.outputNames: vn = vn.split('.', 1) nodeName = vn[0] varName = vn[1] r.append(res['output'][nodeName][varName]) if res['graphError'] != 0: for i in range(len(r)): r[i] = self.failValue ret.append(r) conn.send(['result', stat, ret]) elif msg[0] == 'save': # Save the flow sheet self.dat.save() elif msg[0] == 'scaled': # the default is not scaled input # if you set this before starting workers # you can make set to expect scaled input self.scaled = msg[1] elif msg[0] == 'inputNames': # Set the input variables self.setInputs(msg[1]) conn.send(['inputNames', self.inputNames]) elif msg[0] == 'outputNames': # Set the output variable names. self.setOutputs(msg[1]) conn.send(['outputNames', self.outputNames]) if quitListening: break # do whatever to finish up print("exiting foqus listener") self.listener.close()
class foqusListener2(threading.Thread): """ A multiprocessing listener to allow FOQUS to be controlled over a socket connection. The main purpose for this adaptive sampling. """ def __init__(self, dat, host='localhost', port=56002): threading.Thread.__init__(self) self.daemon = True self.gt = None self.dat = dat # Create a listener self.address = (host, port) self.listener = Listener(self.address) def run(self): quitListening = False while True: #Wait for a connection to be made conn = self.listener.accept() while True: msg = conn.recv() if not (msg and isinstance(msg, list) and msg[0]): # ignore improperly formatted messages continue if msg[0] == "close": # close the connection conn.close() break elif msg[0] == "quit": # close the connection and don't wait for another quitListening = True if self.gt: self.gt.terminate() conn.close() break elif msg[0] == "loadValues": try: self.dat.loadFlowsheetValues(msg[1]) conn.send([0]) except Exception as e: logging.exception("Error loading values") conn.send([1]) elif msg[0] == "run": try: self.gt = self.dat.flowsheet.runAsThread() conn.send([0]) except: logging.exception("Error running flowsheet") conn.send([1]) elif msg[0] == "saveValues": self.gt.join(10) if self.gt.isAlive(): # still waiting but continue so you have a # chance to shutdown the listener if you want conn.send([1, "Still Running"]) else: if self.gt.res: self.dat.flowsheet.loadValues(self.gt.res) else: self.dat.flowsheet.errorStat = 19 self.dat.saveFlowsheetValues(msg[1]) conn.send([0]) if quitListening: break self.listener.close()
class PolicyHandler(XCTRLModule): def __init__(self, config, event_queue, debug, vmac_encoder, loop_detector, test, timing): super(PolicyHandler, self).__init__(config, event_queue, debug) self.logger = logging.getLogger('xctrl') self.logger.info('init') self.test = test self.timing = timing if self.timing: self.timing_file = 'policy_timing_' + str(int( time.time())) + '.log' self.loop_detector = loop_detector self.vmac_encoder = vmac_encoder self.sender = FlowModSender(self.config.refmon_url) self.policies = defaultdict(lambda: defaultdict(list)) self.controller = GSSmT(self.sender, self.config, self.vmac_encoder) self.logger.info('init') self.ingress_participants = defaultdict(set) self.egress_participants = defaultdict(set) self.run = False self.listener = None def start(self): """ Receives policiy installation requests, checks the request with the loop detection module and installs it. :return: """ self.logger.info('start') self.listener = Listener((self.config.policy_handler.address, self.config.policy_handler.port), authkey=None) self.run = True if not self.test: self.controller.start() while self.run: conn = self.listener.accept() tmp = conn.recv() policies = json.loads(tmp) i = 0 if self.timing: start_time = time.clock() for policy in policies: ingress_participant = policy["participant"] egress_participant = policy["action"]["fwd"] type = policy["type"] match = policy["match"] action = policy["action"] if type == "outbound": safe_to_install = self.loop_detector.activate_policy( ingress_participant, egress_participant) else: safe_to_install = True if safe_to_install: if not self.test: cookie = self.controller.add_flow_rule( ingress_participant, type, match, egress_participant) else: cookie = 0 self.policies[ingress_participant][type].append( Policy(cookie, match, action, egress_participant)) # update structures if type == "outbound": self.ingress_participants[egress_participant].add( ingress_participant) self.egress_participants[ingress_participant].add( egress_participant) i += 1 if self.timing: end_time = time.clock() with open(self.timing_file, "a") as outfile: outfile.write(str(end_time - start_time) + '\n') reply = "Total Received Policies: " + str( len(policies)) + " Accepted Policies: " + str(i) conn.send(reply) conn.close() def stop(self): self.logger.info('stop') self.run = False def get_egress_participants(self, ingress_participant): return self.egress_participants[ingress_participant] def get_ingress_participants(self, egress_participant): return self.ingress_participants[egress_participant] def update_policies(self): # after change of supersets, update policies in the dataplane, only outbound policies need to be changed for policies in self.policies.values(): for policy in policies["outbound"]: if not self.test: cookie = self.controller.update_flow_rule( "outbound", policy.cookie, policy.match, policy.forward_participant)
class StreamingDriver(object): def __init__(self, conf): # initialize config params self.batch_interval = conf['batch_interval'] self.window_length = conf['window_length'] self.sliding_interval = conf['sliding_interval'] self.sm_socket = tuple(conf['sm_socket']) self.sm_listener = Listener(self.sm_socket) self.op_handler_socket = conf['op_handler_socket'] self.spark_stream_address = conf['spark_stream_address'] self.spark_stream_port = conf['spark_stream_port'] self.start_time = time.time() self.sc = SparkContext(appName="Sonata-Streaming") self.sc.setLogLevel("OFF") self.ssc = StreamingContext(self.sc, self.batch_interval) def start(self): lines = self.ssc.socketTextStream(self.spark_stream_address, self.spark_stream_port) pktstream = (lines.map(lambda line: processLogLine(line))) print(self.window_length, self.sliding_interval) self.process_pktstream(pktstream) self.ssc.start() self.ssc.awaitTermination() def process_pktstream(self, pktstream): print("pktstream") spark_queries = {} conn = self.sm_listener.accept() raw_data = conn.recv() data = pickle.loads(raw_data) queries = data['queries'] join_queries = data['join_queries'] for queryId in queries: query = queries[queryId] if not query.has_join and queryId not in join_queries: query_str = "pktstream.window(self.window_length, self.sliding_interval).transform(lambda rdd: (rdd.filter(lambda p : (p[1]==str('" + str( queryId ) + "'))).map(lambda p : (p[2:]))." + query.compile( ) + ")).foreachRDD(lambda rdd:send_reduction_keys(rdd, " + str( self.op_handler_socket) + "," + str( self.start_time) + ",\'" + str(queryId) + "\'))" print(query_str) spark_queries[queryId] = eval(query_str) elif not query.has_join and queryId in join_queries: query_str = "pktstream.window(self.window_length, self.sliding_interval).transform(lambda rdd: (rdd.filter(lambda p : (p[1]==str('" + str( queryId ) + "'))).map(lambda p : (p[2:]))." + query.compile( ) + "))" #.foreachRDD(lambda rdd:send_reduction_keys(rdd, " + str(self.op_handler_socket) + "," + str(self.start_time) + ",\'" + str(queryId) + "\'))" print(query_str) spark_queries[queryId] = eval(query_str) else: query_str = query.compile( ) + ".foreachRDD(lambda rdd: print(\"Join \" + str(rdd.take(5))))" print(query_str) spark_queries[queryId] = eval(query_str)
def main(): # write PID to file with open('player_pidfile', 'w') as pidfile: print >> pidfile, getpid() # check length of arguments; # 1 = error, 2 = idle, 3 = media, 4 = media w/modifier stream_id = None media_uri = None live_source = False if len(argv) < 2: logging.critical('error: no stream ID specified.') sys_exit(4) stream_id = argv[1] if len(argv) > 2: # media; capture media_uri media_uri = argv[2] if len(argv) > 3 and argv[3] == 'live': live_source = True # import config global_config = None with open('config.yaml', 'r') as config_file: global_config = load_yaml(config_file) logging.info('configuration file loaded and parsed.') if type(global_config) is not dict: logging.critical( 'error: configuration file parsed into invalid type.') sys_exit(2) if len(global_config) <= 0: logging.critical( 'error: configuration file parsed into empty object.') sys_exit(3) # craft process title from name (p-{name}) process_title = 'pp-{}'.format(global_config['name']) # set loglevel target_lvl = global_config['log_level'] if hasattr(logging, target_lvl): logging.getLogger().setLevel(getattr(logging, target_lvl)) # set lock to prevent concurrency and set proctitle global lock_socket lock_socket = socket(AF_UNIX, SOCK_DGRAM) try: lock_socket.bind('\0{}'.format(process_title)) logging.info('got process lock') except socket_error: logging.critical('failed to get process lock; already running?') sys_exit(1) # set custom process title setproctitle(process_title) # declare now to allow access by _exit state = None runtime = None conn = None listener = None def _exit(): logging.debug( 'stopping player, closing control connection, and exiting') if runtime: # 0: init 1: started 2: stopped if state.value == 1: runtime.stop() if conn: conn.close() if listener: listener.close() # handle signals gracefully def _exit_on_signal(signal, frame): logging.warning('caught signal {}'.format(str(signal))) _exit() signal(SIGABRT, _exit_on_signal) signal(SIGINT, _exit_on_signal) signal(SIGHUP, _exit_on_signal) signal(SIGQUIT, _exit_on_signal) signal(SIGTERM, _exit_on_signal) state = Value('i', 0) # create new runtime object based on type runtime = None if media_uri: runtime = Player(global_config['SquishPlayer'], state, stream_id, media_uri, live_source) else: runtime = Idler(global_config['SquishPlayer'], state, stream_id) # set up listener for comms with bot address = global_config['control_socket_file'] listener = Listener(address, authkey='phoebe') # block on connection from bot logging.debug( 'awaiting control connection on socket at {}'.format(address)) conn = listener.accept() # connection made; start runtime (runs in new thread) logging.debug('connection accepted') runtime.start() # enter command loop (in this thread) while True: # exit if player mainloop no longer running # 0: init 1: started 2: stopped if state.value == 2: logging.info('player thread no longer alive') break # check for a command if not conn.poll(1): continue try: # wait for a command command = conn.recv() except (EOFError, IOError): logging.error('Error encountered when attempting ' 'to receive from control connection') break # parse into name/optional args cmd_name = command[0].lower() cmd_arg = None if len(command) > 1: cmd_arg = command[1] # execute command actions if cmd_name == 'play': runtime.play() # stop player and exit elif cmd_name == 'stop': logging.debug('stopping player on command') break # retrieve current position, duration elif cmd_name == 'getpos': if hasattr(runtime, 'get_play_position'): pos = runtime.get_play_position() if pos: conn.send(['OK', pos]) else: conn.send(['ERROR', 'no position available']) else: conn.send(['ERROR', 'getpos not supported by active runtime']) elif cmd_name == 'getlivepos': if hasattr(runtime, 'get_live_play_position'): pos = runtime.get_live_play_position() if pos: conn.send(['OK', pos]) else: conn.send(['ERROR', 'no live position available']) else: conn.send( ['ERROR', 'getlivepos not supported by active runtime']) # seek by specified amount elif cmd_name == 'seek': if hasattr(runtime, 'seek'): if cmd_arg: result = runtime.seek(cmd_arg) if result: conn.send(['OK']) else: conn.send(['ERROR', 'seek failed']) else: conn.send(['ERROR', 'seek not supported by active runtime']) # jump to specified position elif cmd_name == 'jump': if hasattr(runtime, 'seek'): error = False if cmd_arg: pos = runtime.get_play_position() if pos: jump_to = cmd_arg - pos[0] result = runtime.seek(jump_to) if result: conn.send(['OK']) else: error = True else: error = True if error: conn.send(['ERROR', 'jump failed']) else: conn.send(['ERROR', 'jump not supported by active runtime']) # out of command loop; clean up and exit logging.debug('exited command loop') _exit()
from multiprocessing.connection import Listener address = ("localhost", 8080) authkey = b"qwerty" # 用来进行hmac认证,注意是bytes类型 server = Listener(address, authkey=authkey) conn = server.accept() # 接受连接 while True: data = conn.recv() # 接收数据 print(data.shape)
class InternalDeferredPlugin(object): def __init__(self, config): self.ida_path = config.getoption('--ida') self.ida_file = config.getoption('--ida-file') self.config = config self.session = None self.listener = Listener() self.conn = None self.logfile = tempfile.NamedTemporaryFile(delete=False) self.stop = False def ida_start(self): internal_script = os.path.join(os.path.dirname(__file__), "idaworker_main.py") script_args = '{}'.format(self.listener.address) args = [ self.ida_path, # autonomous mode. IDA will not display dialog boxes. # Designed to be used together with -S switch. "-A", "-S\"{}\" {}".format(internal_script, script_args), "-L{}".format(self.logfile.name), # Load user-provided or start with an empty database self.ida_file if self.ida_file else "-t" ] log.debug("worker execution arguments: %s", args) self.proc = subprocess.Popen(args=args) # accept a single connection self.conn = self.listener.accept() self.listener.close() self.listener = None def ida_finish(self, interrupted): if interrupted: log.warning( "Abrupt termination of external test session. worker " "log: %s", self.logfile.read()) log.info("Stopping...") self.proc.poll() if self.proc.returncode is None: self.proc.kill() self.stop = True def command_ping(self): self.send('ping') self.recv('pong') def command_dependencies(self): plugins = [] if (hasattr(self.config.option, 'cov_source') and self.config.option.cov_source): plugins.append("pytest_cov") self.send('dependencies', 'check', *plugins) if self.recv('dependencies') == ('ready', ): return self.send('dependencies', 'install', *plugins) self.recv('dependencies', 'ready') def command_autoanalysis_wait(self): self.send('autoanalysis', 'wait') self.recv('autoanalysis', 'done') def command_configure(self, config): option_dict = copy.deepcopy(vars(config.option)) # block interfering plugins option_dict['plugins'].append("no:cacheprovider") option_dict['plugins'].append("no:pytest-qt") option_dict['plugins'].append("no:xdist") option_dict['plugins'].append("no:xvfb") option_dict['usepdb'] = False # cleanup our own plugin configuration option_dict["plugins"].append("no:idapro") del option_dict['ida'] del option_dict['ida_file'] if platform.system() == "Windows": # remove capturing, this doesn't properly work in windows option_dict["plugins"].append("no:terminal") option_dict["capture"] = "sys" self.send('configure', config.args, option_dict) self.recv('configure', 'done') def command_cmdline_main(self): self.send('cmdline_main') self.recv('cmdline_main', 'start') def command_collect(self): self.recv('collection', 'start') self.config.hook.pytest_collectstart() while True: r = self.recv('collection') if r[0] == 'report': report = self.deserialize_report("collect", r[1]) self.config.hook.pytest_collectreport(report=report) elif r[0] == 'finish': collected_tests = r[1] self.session.testscollected = len(collected_tests) self.config.hook.pytest_collection_finish(session=self.session) break elif r[0] == 'modifyitems': self.config.hook.pytest_collection_modifyitems( session=self.session, config=self.config, items=r[1]) else: raise RuntimeError("Invalid collect response received: " "{}".format(r)) def command_runtest(self): self.recv('runtest', 'start') while True: r = self.recv('runtest') if r[0] == 'logstart': self.config.hook.pytest_runtest_logstart(nodeid=r[1], location=r[2]) elif r[0] == 'logreport': report = self.deserialize_report("test", r[1]) self.config.hook.pytest_runtest_logreport(report=report) elif r[0] == 'logfinish': # the pytest_runtest_logfinish hook was introduced in pytest3.4 if hasattr(self.config.hook, 'pytest_runtest_logfinish'): self.config.hook.pytest_runtest_logfinish(nodeid=r[1], location=r[2]) elif r[0] == 'finish': break else: raise RuntimeError("Invalid runtest response received: " "{}".format(r)) def command_quit(self): self.send('quit') self.recv('quitting') def send(self, *s): log.debug("Sending: %s", s) return self.conn.send(s) def recv(self, *args): try: while not self.conn.poll(1): if self.stop: raise KeyboardInterrupt r = self.conn.recv() log.debug("Received: %s", r) except Exception: log.critical("Exception during receive, worker output: %s", self.logfile.read()) raise if args and r[:len(args)] != args: raise RuntimeError("Invalid response recieved; while expecting " "'{}' got '{}'".format(args, r)) return r[len(args):] def deserialize_report(self, reporttype, report): from _pytest.runner import TestReport, CollectReport from pytest import Item if 'result' in report: newresult = [] for item in report['result']: item_obj = Item(item['name'], config=self.config, session=self.session) newresult.append(item_obj) report['result'] = newresult if reporttype == "test": return TestReport(**report) elif reporttype == "collect": return CollectReport(**report) else: raise RuntimeError("Invalid report type: {}".format(reporttype)) def pytest_runtestloop(self, session): self.session = session try: self.ida_start() self.command_ping() self.command_dependencies() self.command_autoanalysis_wait() self.command_configure(self.config) self.command_cmdline_main() self.command_collect() self.command_runtest() exitstatus = self.recv('session', 'finish') # TODO: The same exit status will be derived by pytest. might be # useful to make sure they match del exitstatus self.recv('cmdline_main', 'finish') self.command_quit() except Exception: self.ida_finish(True) raise return True def pytest_sessionfinish(self, exitstatus): self.ida_finish(exitstatus == 2) # EXIT_ITERRUPTED @staticmethod def pytest_collection(): # prohibit collection of test items in master process. test collection # should be done by a worker with access to IDA modules return True