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 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()
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 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 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)
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()
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 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()
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 _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 close(self, blockUntilClosed = True): '''close may wait 1 second.''' Listener.close(self) if self.establish_connection_to_close_lock.acquire(False): if not blockUntilClosed: thread_start_new(self._connect_to_pipe_to_close_it, ()) else: self._connect_to_pipe_to_close_it()
def echo_server(address, authkey): serv = Listener(address, authkey=authkey) while True: try: client = serv.accept() echo_client(client) except Exception: traceback.print_exc()
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 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)
class SingleInstanceApp(BaseJobManager): def __init__(self, name, session=''): BaseJobManager.__init__(self) if sys.platform == 'win32': self.address = r'\\.\pipe\single_instance_app.' else: self.address = '/var/tmp/single_instance_app.pipe' trans = string.maketrans('\\ ', '__') self.address += '.'+name.translate(trans) if session and len(session)>0: self.address += session.translate(trans) self.accepting_connection = False def _accept_connection(self): while self.accepting_connection: conn = self.listener.accept() args = conn.recv() conn.close() self.queue_launch_instance(*args) def try_start_instance(self, *args): try: conn = Client(self.address, authkey='secret password') conn.send(args) conn.close() #print "sent my args(%s)"%(', '.join(args)) return False except: self.queue_launch_instance(*args) self.listener = Listener(self.address, authkey='secret password') self.accepting_connection = True self.listener_tread = threading.Thread(target=self._accept_connection) self.listener_tread.daemon = True #because of listener.accept() blocks self.listener_tread.start() return True def stop_accepting(self): self.accepting_connection = False self.listener.close() self.listener_tread.join(0.01) def queue_launch_instance(self, *args): fancy_log(sys.stdout, 'queued instance launch with args: '+str(args)) self.add_job(*args) #override #def do_the_job(self, *args): # print 'calls instance with args: ',args # self.update_job() def job_is_updated(self, *args, **kwargs): fancy_log(sys.stdout,'calls instance with args: '+str(args)+'\n kwargs: '+str(kwargs)) pass
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 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 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 __init__ (self): from app import APP self.logger = APP.LoggingClient(name="UIHUB") addr = APP.Functions.parse_socket_address ( APP.BE.CLI.server_address ) try: os.unlink (addr) except: pass self.logger( "Starting. Listening on {0}".format (addr) ) Listener.__init__(self, addr) self.queue = ThreadingQueue.Queue(1024) self.workers = [CLIWorker(self.queue, self.logger) for i in range(0,5)]
class server(): def __init__(self, sock=8000, ip='localhost', authkey='secret password', MAX=3): self.sock = sock self.ip = ip self.authkey = authkey self.connections = [] self.scale = MAX self.jobs = [] self.outputs = [] self.socket = None def setScale(self, scale): self.scale = scale def addJobs(self, _list): self.jobs.extend(_list) def _listen(self): scale = self.scale while True: if scale: scale -= 1 conn = self.socket.accept() self.connections.append(conn) #print 'connection accepted from', conn.last_accepted else: break def start(self): self.socket = Listener((self.ip, self.sock), authkey=self.authkey) self._listen() next = self.scale-1 scheduler = chunks(self.jobs, self.scale) if len(self.jobs) is 0: print "Please add some jobs before you start me !!" return for each in scheduler: self.connections[next].send(each) next -= 1 if next < 0: break now = time.time() for conn in self.connections: x = conn.recv() self.outputs.extend(x) conn.close() self.socket.close() t = time.time() - now X = [] X.append(["time", t]) print tabulate(X, tablefmt='rst') #print str("Took me " + str(t) + " time") return self.outputs
def transient_server(address, authkey, seconds): class TimeoutException(Exception): pass def timeout_handler(signum, frame): raise TimeoutException('end for listener') signal.signal(signal.SIGALRM, timeout_handler) server = Listener(address, authkey=authkey) try: yield server except TimeoutException: server.close()
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 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 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 run(self): address = ("localhost", 6000) listener = Listener(address, authkey="secret password") conn = listener.accept() while True: command = self.queue.get() conn.send_bytes(command) self.queue.task_done() if command == "quit": break conn.close() listener.close()
class Visualizer(object): def __init__(self, din = {}): import plotter self.dict = { 'plotter': plotter.SubplotAnimation(), } self.dict.update(din) print "[IPC-S] Server process ID ", os.getpid() self.init_communication() self.__run__() def init_communication(self): print "[IPC-S] Opening Server socket ... ", address = ('localhost', 6000) # family is deduced to be 'AF_INET' self.listener = Listener(address) print "done!" print "[IPC-S] Waiting Client ... ", self.conn = self.listener.accept() print 'connection accepted from', self.listener.last_accepted def close_communication(self): print "[IPC-S] Closing interprocess communication ... ", self.conn.close() self.listener.close() print "done!" def read_msg(self): return self.conn.recv() def __run__(self): while True: msg = self.read_msg() # msg format: [os.getpid(), i, time, avg[0], avg[1], avg[2]] # Process the message if msg == 'close': self.close_communication() break procID = msg[0] counter = msg[1] logTime = msg[2] avg = msg[3:] #print os.getpid(), "--- Recived ", msg, " ||| avg ", avg, " ||| counter ", counter # Plot the data self.dict['plotter'].draw_frame([logTime, avg[0], avg[1], avg[2]] ) # plots a point every 100 ms
def run(self): try: server_c = Listener(self.address, authkey=self.authkey) while True: client_c = server_c.accept() clientaddress = server_c.last_accepted handler = ClientHandler(client_c, self.lockSettingsInstance, clientaddress) handler.setOutputFrequencySignal.connect( self.lockSettingsInstance.setOutputFrequencyGui ) handler.setHarmonicSignal.connect(self.lockSettingsInstance.setHarmonicGui) handler.start() except Exception as e: logging.getLogger(__name__).exception(e)
class server(object): def __init__(self, logger, endpoint=('localhost', 6000), authkey=None): self.logger = logger self.listener = Listener(endpoint, authkey=authkey) self.port = endpoint[1] self.sender_queue = Queue() self.receiver_queue = Queue() def start(self): self.logger.debug('waiting for connection') self.conn = self.listener.accept() self.logger.debug('Connection accepted from ' + str(self.listener.last_accepted)) self.sender = Thread(target=_sender, args=(self.conn, self.sender_queue)) self.sender.setName("sender server " + str(self.port)) self.sender.start() self.receiver = Thread(target=_receiver, args=(self.conn, self.receiver_queue)) self.receiver.setName("receiver server " + str(self.port)) self.receiver.start()
def main(args=sys.argv): gui_debug = None if args[0] == '__CALIBRE_GUI_DEBUG__': gui_debug = args[1] args = ['calibre'] try: app, opts, args, actions = init_qt(args) except AbortInit: return 1 from calibre.utils.lock import singleinstance from multiprocessing.connection import Listener si = singleinstance('calibre GUI') if si and opts.shutdown_running_calibre: return 0 if si: try: listener = Listener(address=gui_socket_address()) except socket.error: if iswindows: cant_start() if os.path.exists(gui_socket_address()): os.remove(gui_socket_address()) try: listener = Listener(address=gui_socket_address()) except socket.error: cant_start() else: return run_gui(opts, args, actions, listener, app, gui_debug=gui_debug) else: return run_gui(opts, args, actions, listener, app, gui_debug=gui_debug) otherinstance = False try: listener = Listener(address=gui_socket_address()) except socket.error: # Good si is correct (on UNIX) otherinstance = True else: # On windows only singleinstance can be trusted otherinstance = True if iswindows else False if not otherinstance and not opts.shutdown_running_calibre: return run_gui(opts, args, actions, listener, app, gui_debug=gui_debug) communicate(opts, args) return 0
def main(): firebase_connector = FirebaseConnector() log("Firebase connection ready") visitor_info = firebase_connector.get_visitor_info() log("Got visitor info") if not CONFIG["super-debug"]["dont_redownload_visitor_photos"]: firebase_connector.get_visitor_photos(visitor_info) log("Finished getting visitor photos") else: log("Skipping getting visitor photos per config") if not CONFIG["super-debug"]["dont_reprocess_visitor_faces"]: known_face_encodings, known_face_names = process_visitor_images(visitor_info) else: log("Attempting to skip processing visitor faces per config") try: visitor_faces_pickle = open( CONFIG["super-debug"]["visitor_faces_pickle"], 'rb' ) saved_face_data = pickle.load(visitor_faces_pickle) known_face_encodings, known_face_names = saved_face_data log("Loaded saved visitor face data") except FileNotFoundError: log("Failed to load visitor face data, generating and saving it") known_face_encodings, known_face_names = process_visitor_images(visitor_info) visitor_faces_pickle = open( CONFIG["super-debug"]["visitor_faces_pickle"], 'wb' ) face_data = (known_face_encodings, known_face_names) pickle.dump(face_data, visitor_faces_pickle) try: os.unlink('/tmp/buzz_socket') except FileNotFoundError: pass while True: with Listener(CONFIG["core"]["socket"], "AF_UNIX") as listener: log("Ready to accept connections") with listener.accept() as conn: received_signal = conn.recv() if CONFIG["core"]["debug"]: log(f"Received signal: {received_signal}") if received_signal == "event_start": handle_motion(firebase_connector, known_face_encodings, known_face_names) elif received_signal == "event_end" and CONFIG["core"]["debug"]: notification_response = firebase_connector.send_notification( "DEBUG: Motion event ended.") log(f"Notification {notification_response} sent") elif "movie_end" in received_signal: log(f"Video file ended {received_signal.split(' ')[1]}") firebase_connector.upload_to_storage(received_signal.split(' ')[1]) log(f"Uploaded video file to Firebase Storage")
def receive_loop(self): try: address = ('localhost', 6001) listener = Listener(address, authkey=b'password') conn = listener.accept() while self.thread: msg = conn.recv() if isinstance(msg, str): t = threading.Thread(target=self.r, args=(msg, )) # t.start() else: send = msg.pop('send') read = msg.pop('read') stream = msg.pop('stream') print("send: ", send) print("read: ", read) print("stream: ", stream) address = msg.pop('mac_addr', None) if address is not None: # self.last_activity = time.time() if not self.is_connected_to_device(address): self.connect_to_device(address) if send and stream and not read: t = threading.Thread(target=self.r, args=(address, )) t.start() elif send and not read: data = msg t = threading.Thread(target=self.s, args=(address, data)) t.start() elif send and read: data = msg t = threading.Thread(target=self.s_t_r, args=(address, data)) t.start() listener.close() except Exception as e: print(e)
def listen(): listener = Listener(('localhost', 6000), authkey=b'secret') conn = listener.accept() while True: msg = conn.recv() print("received: {}".format(msg)) if msg == 'close': break if msg[0] == 'video': subprocess.Popen(['vlc', msg[1], '--fullscreen']) elif msg[0] == 'website': subprocess.Popen(['firefox', msg[1]]) elif msg[0] == 'video-pause': requests.get('http://127.0.0.1:8080/requests/status.xml?command=pl_pause') elif msg[0] == 'video-stop': requests.get('http://127.0.0.1:8080/requests/status.xml?command=pl_stop')
def server(address, port): server = Listener(address, authkey=b"peekaboo") conn = server.accept() worker_pid = conn.recv() print(f"Server : Worked PID {worker_pid}") # This is how socket.socket() is generally used to return new socket fd s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) s.bind(('', port)) s.listen(1) while True: client, address = s.accept() print(f"Server got connection from {address}") send_handle(conn, client.fileno(), worker_pid) client.close()
def bare_server(port, logfile): """Bare-bones remote server""" from multiprocessing.connection import Listener fd = open(logfile, "w", 0) address = ("", port) listener = Listener(address) conn = listener.accept() fd.write("Connection accepted\n") # Wait for main server and execute it msg = conn.recv() try: exec(msg) except Exception as e: fd.write("ERROR: %r\n" % e) fd.close() listener.close()
def __init__(self, port, hostname=None, **kwargs): address = ('localhost', port) # family is deduced to be 'AF_INET' if hostname is None: hostname = "{}:{}".format(socket.gethostname(), port) self.hostname = hostname self.listener = Listener(address, authkey='secret password') Thread(target=self.accept_wait, name="accept_wait", args=(self.listener,)).start()
def __init__(self, job_handler: Callable[[TJob], None], query_handler: Callable[[TQuery], Any], max_queue_size=50, timeout=5): """ @param job_handler: The callback for when a new job is received @param query_handler: The callback for when a new query is received @param max_queue_size: The maximum number of instructions in the queue before the next one is dropped @param timeout: The amount of time to wait to place a job/query on the queue before dropping it (if full) """ self.job_handler = job_handler self.query_handler = query_handler self.listener = Listener(Config.address) self.connection = None self.job_queue = Queue(maxsize=max_queue_size) # Possible issue: if queries are dropped, the server may become out of sync with the worker, # as is expects are response for each query and is not guaranteed one self.query_queue = Queue(maxsize=max_queue_size) self.timeout = timeout
def start(self): # setup listener logger.info('Server starting %s at %s', os.getpid(), SERVER_ADDRESS) #~ authkey = multiprocessing.current_process().authkey #~ fh = open(AUTHKEY_FILE.path, 'w') #~ fh.write(authkey) #~ fh.close() #~ print "SERVER >>%s<<" % authkey if SERVER_ADDRESS_FAMILY == 'AF_UNIX' \ and os.path.exists(SERVER_ADDRESS): # Clean up old socket (someone should already have checked # before whether or not it is functional) os.unlink(SERVER_ADDRESS) #~ self.listener = Listener(SERVER_ADDRESS, authkey=authkey) self.listener = Listener(SERVER_ADDRESS)
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 handler(): this.LOGGER.info('START RECEIVER') address = ('localhost', 6000) # family is deduced to be 'AF_INET' this.listener = Listener(address, authkey=b'12345') this.LOGGER.info('START ACCEPT') this.conn = this.listener.accept() this.LOGGER.info('START THREADING') thread.start_new_thread(messageHandler, (this.conn))
def testing(): from multiprocessing.connection import Listener address = ('localhost', 6000) # family is deduced to be 'AF_INET' listener = Listener(address, authkey='dp23y4fm') while True: conn = listener.accept() if conn.recv_bytes() == "stop": print "Stopping Speeking" global is_stopped with lock: is_stopped = True conn.close() listener.close()
def run(self): with Listener(*constants.XWARED_SOCKET) as listener: while True: with listener.accept() as conn: func, args, kwargs = conn.recv() response = getattr(self._xwared, "interface_" + func)(*args, **kwargs) conn.send(response)
class StreamReceiver: def __init__(self, streamq): self.streamq = streamq self.run() def openconnection(self): address = ('localhost', 3140) print "[INIT] Opening connection on %s)" % str(address) self._LISTENER = Listener(address, authkey='1kdw91l') self._CONN = self._LISTENER.accept() return self._CONN def run(self): while True: try: conn = self.openconnection() self.serve(conn) except EOFError: conn.close() self._LISTENER.close() print "Connection terminated by client" def serve(self, conn): while True: msg = conn.recv() if msg == 'TERMINATE': print 'terminate received' self.finalize() conn.send("OK") conn.close() break resp = self.dispatch(msg) conn.send(resp) # expects format: {msg:'BLOCK', block:DATA} def dispatch(self, msg): cmd = msg['msg'] data = msg['block'] if cmd == 'BLOCK': self.streamq.addblock(data) return 'OK' return 'NOK' def finalize(self): pass
def _server(self): logger.info('Starting OM messaging service...') self._stop = False while not self._stop: try: conn = self.listener.accept() logger.info('connection accepted from {0}'.format(self.listener.last_accepted)) receiver = Thread(target=self._receiver, args=(conn,)) receiver.daemon = True receiver.start() except IOError as io_error: logger.error('IOError in accepting connection: {0}'.format(io_error)) except Exception as e: logger.exception('Error in message service. Restarting...') self.listener.close() time.sleep(1) self.listener = Listener(self.address, authkey=self.authkey)
def server(address, port, t, ang, fes, start_time, running, imu_data): serv = Listener((address, port)) # s = socket.socket() # s.bind(address) # s.listen() while running.value: # client, addr = s.accept() client = serv.accept() print('Connected to {}'.format(serv.last_accepted)) source = client.recv() print('Source: {}'.format(source)) p = multiprocessing.Process(target=do_stuff, args=(client, source, t, ang, fes, start_time, running, imu_data)) # do_stuff(client, addr) p.start() # signal.pause() print('End of process server')
def server(): address = ('localhost', 6000) # family is deduced to be 'AF_INET' with Listener(address, authkey=b'secret password') as listener: with listener.accept() as conn: print('connection accepted from', listener.last_accepted) conn.send([2.25, None, 'junk', float]) conn.send_bytes(b'hello') conn.send_bytes(array('i', [42, 1729]))
def server(work_address, port): # Wait for the worker to connect work_serv = Listener(work_address, authkey=b'peekaboo') worker = work_serv.accept() worker_pid = worker.recv() # Now run a TCP/IP server and send clients to worker s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) s.bind(('', port)) s.listen(1) while True: client, addr = s.accept() print('SERVER: Got connection from', addr) send_handle(worker, client.fileno(), worker_pid) client.close()
def _recv_align_result(host): """ Receive align result from receiver. :param host: str. The host who is waiting for align result. The host is represented as "id:ip:port". :return: set. The received align result. """ from multiprocessing.connection import Listener ip_addr = host.split(":")[1] port = int(host.split(":")[2]) server = Listener((ip_addr, port)) conn = server.accept() result = conn.recv() conn.close() server.close() return result
def exploration(): """Plot q-table and game board for reference during exploration.""" fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(10, 5)) plt.ion() plt.show() address = ('localhost', 6000) # family is deduced to be 'AF_INET' listener = Listener(address, authkey=b'secret password') conn = listener.accept() print('connection accepted from {}'.format(listener.last_accepted)) while True: msg = conn.recv() ax1.clear() ax2.clear() q_table = msg['q_table'] game_board = msg['game_board'] cords = msg['cords'] action = msg['action'] next_state = map_action_to_next_state(cords, action) print('State: {} action: {} next {}'.format( cords, action, next_state)) next_row = int(next_state[1]) next_col = next_state[0] row = int(cords[1]) col = cords[0] game_board.loc[row, col] = -3 # Mark current state on a game board game_board.loc[next_row, next_col] = -2 # mark next state on a game board # Plot q-table and game board as heatmaps sns.heatmap(q_table, ax=ax1, cbar=False, vmin=-200, vmax=200, annot=True) sns.heatmap(game_board, ax=ax2, cbar=False, vmin=-3, vmax=3, annot=True) plt.draw() plt.pause(0.1)
def run(self): global PROCESS_STATUS listener = Listener((self.ip, self.port)) while True: try: conn = listener.accept() new_status = int(conn.recv()) if new_status in [ PROCESS_STATUS_RUN, PROCESS_STATUS_PAUSE, PROCESS_STATUS_FINISH, PROCESS_STATUS_STOP ]: PROCESS_STATUS = new_status except IOError: pass finally: conn.close() listener.close()
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")
class Server: def __init__(self, camera, board_length, board_width, board_orientation=None, debug=False): self.camera = camera self.debug = debug # Listener self.address = 'localhost' #self.address = '0.0.0.0' if board_orientation == 'left': self.port = 6001 elif board_orientation == 'right': self.port = 6002 else: self.port = 6000 self.listener = Listener((self.address, self.port), authkey=b'secret password') # Boards suffix = '' if board_orientation is None else '_{}'.format(board_orientation) self.robot_board = Board('robots' + suffix, board_length, board_width, board_orientation, 0.036) self.cube_board = Board('cubes' + suffix, board_length, board_width, board_orientation, 0.012) def initialize_boards(self): boards = [self.robot_board, self.cube_board] for board in boards: board.reset() while not all(board.initialized for board in boards): image = self.camera.read() for board in boards: if not board.initialized: board.initialize(image, debug=self.debug) def run(self): try: while True: # Connect to client print('Waiting for connection ({}:{})'.format(self.address, self.port)) conn = self.listener.accept() print('Connected!') # Initialize boards self.initialize_boards() # Send pose estimates debug_data = None while True: start_time = time.time() try: debug_data = conn.recv() # Wait for request image = self.camera.read() robot_poses = self.robot_board.get_marker_poses(image, debug_data=debug_data, debug=self.debug) cube_poses = self.cube_board.get_marker_poses(image, debug_data=debug_data, debug=self.debug) conn.send((robot_poses, cube_poses)) except (ConnectionResetError, EOFError): break print('{:.1f} ms'.format(1000 * (time.time() - start_time))) finally: self.camera.release() cv2.destroyAllWindows()
class MJPEGServer: def __init__( self, input_address: _Address, http_address: _Address, max_fps: Optional[int] = None, ): self._publisher = Publisher() self._http_server = _MJPEGHTTPServer(http_address, self._publisher, max_fps) self._listener = Listener(input_address) @property def input_address(self): return self._listener.address @property def http_address(self): return self._http_server.server_address def _listener_thread(self): try: with self._listener: while True: logger.debug("Waiting for data source to connect...") conn = self._listener.accept() logger.info("Source connection accepted.") receiver = threading.Thread(target=self._receiver_thread, args=(conn, ), daemon=True) receiver.start() time.sleep(1) except: # pragma: no cover self.shutdown() traceback.print_exc() def _receiver_thread(self, conn: Connection): with conn: while True: try: buf = conn.recv_bytes() except EOFError: break channel, data = unpack_message(buf) logger.debug("Received data from %s.", channel) self._publisher.publish(channel, data) logger.info("Source connection closed.") def serve_forever(self, poll_interval: float = 0.5) -> None: listener_thread = threading.Thread(target=self._listener_thread, daemon=True) listener_thread.start() self._http_server.serve_forever(poll_interval=poll_interval) def shutdown(self): self._http_server.shutdown()
def run(self): print ('MSocket receiver thread starts...') while not self.terminate: try: address = ('localhost', self.port) # family is deduced to be 'AF_INET' listener = Listener(address, authkey = b'your_password') print ("Listening on port {}..".format(self.port)) conn = listener.accept() # blocking print ('Connection accepted from {}'.format (listener.last_accepted)) except Exception as e: print("MSocket error. Quitting :") print (e) return while not self.terminate: try: self.message = conn.recv() # blocking self.data_ready = True print (self.message) if self.message == 'exit': # quit inner loop break except Exception as e: print(e) break print ('closing client connection..') conn.close() listener.close() if self.message == 'exit': # quit outer loop break print ('shutting down Msock server..') conn.close() listener.close() print ('MSocket receiver thread exits.')
def run(self): """ The supervisor process starts all its assigned processes as its children and monitors their work. """ # Double-fork allows background running if fork() != 0: return # Declare signal handler signal(SIGTERM, self.sighandler) signal(SIGINT, self.sighandler) # Create PID file with open(AppConfig.PID_LOCATION, 'w') as pidfile: pidfile.write(str(getpid())) # Create UNIX_AF listening thread unixsock = Listener(AppConfig.UNIX_SOCKET, 'AF_UNIX') # Spawn listener thread UnixListener(unixsock, self.assigned).start() # Main Loop while self.canSupervise(): # Monitor assigned consumers every 10 sec self.monitInstances() # Sleep 10 sec time.sleep(1) print('Supervisor: Stoping all children....') # Stop all running consumers for process in self.assigned.keys(): while len(self.assigned[process]['running']) > 0: self.stopInstance(process) time.sleep(0.2) # Close unixsocket unixsock.close() # Close everything / remove PID file unlink(AppConfig.PID_LOCATION)
def issue_credential(data): address = ('localhost', Constant.PORT_NUMBER) listener = Listener(address) listener._listener._socket.settimeout(Constant.TIMEOUT) proposal_response = send_offer(data) credential_exchange_id = proposal_response["credential_exchange_id"] logger.info(f'credential_exchange_id: {credential_exchange_id}') while True: try: conn = listener.accept() msg = conn.recv() state = msg["state"] logger.info(f'waiting state change, current state is: {state}') conn.close() if state == 'credential_acked': print("Congratulation, execute publish successfully!") listener.close() break else: time.sleep(0.5) except socket.timeout as e: logger.error(e) print( "vsw: error: request timeout, there might be some issue during publishing" ) listener.close() break
def main(host='localhost', port=6000, authkey='secret password'): "Debug a script and accept a remote frontend" if not sys.argv[1:] or sys.argv[1] in ("--help", "-h"): print "usage: pdb.py scriptfile [arg] ..." sys.exit(2) mainpyfile = sys.argv[1] # Get script filename if not os.path.exists(mainpyfile): print 'Error:', mainpyfile, 'does not exist' sys.exit(1) del sys.argv[0] # Hide "pdb.py" from argument list # Replace pdb's dir with script's dir in front of module search path. sys.path[0] = os.path.dirname(mainpyfile) from multiprocessing.connection import Listener address = (host, port) # family is deduced to be 'AF_INET' listener = Listener(address, authkey=authkey) print "qdb debugger backend: waiting for connection at", address conn = listener.accept() print 'qdb debugger backend: connected to', listener.last_accepted # create the backend qdb = Qdb(conn, redirect_stdio=True, allow_interruptions=True) try: print "running", mainpyfile qdb._runscript(mainpyfile) print "The program finished" except SystemExit: # In most cases SystemExit does not warrant a post-mortem session. print "The program exited via sys.exit(). Exit status: ", print sys.exc_info()[1] raise except Exception: traceback.print_exc() print "Uncaught exception. Entering post mortem debugging" info = sys.exc_info() qdb.post_mortem(info) print "Program terminated!" finally: conn.close() listener.close() print "qdb debbuger backend: connection closed"
def run(self): listener = Listener(('localhost', 18273)) connection = listener.accept() # uuid connection.recv_bytes().decode('utf-8') # ServerBreaks connection.recv_bytes().decode('utf-8') # Empty breaks connection.send_bytes(b'{}') # Continuing if self.stops: connection.recv_bytes().decode('utf-8') connection.send_bytes(b'Continue') self.lock.acquire() connection.close() listener.close() self.lock.release()