Python asyncio.start_unix_server() Examples
The following are 17
code examples of asyncio.start_unix_server().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
asyncio
, or try the search function
.
Example #1
Source File: server.py From aioconsole with GNU General Public License v3.0 | 6 votes |
def start_interactive_server( factory=console.AsynchronousConsole, host=None, port=None, path=None, banner=None, *, loop=None ): if (port is None) == (path is None): raise ValueError("Either a TCP port or a UDS path should be provided") if port is not None: # Override asyncio behavior (i.e serve on all interfaces by default) host = host or "localhost" start_server = partial(asyncio.start_server, host=host, port=port) else: start_server = partial(asyncio.start_unix_server, path=path) client_connected = partial(handle_connect, factory=factory, banner=banner) server = await start_server(client_connected, loop=loop) return server
Example #2
Source File: endpoint.py From lahja with MIT License | 6 votes |
def _start_server(self, ipc_path: Path) -> None: """ Start serving this :class:`~lahja.endpoint.asyncio.AsyncioEndpoint` so that it can receive events. Await until the :class:`~lahja.endpoint.asyncio.AsyncioEndpoint` is ready. """ if not self.is_running: raise RuntimeError(f"Endpoint {self.name} must be running to start server") elif self.is_serving: raise RuntimeError(f"Endpoint {self.name} is already serving") self._ipc_path = ipc_path self._serving = True self._server = await asyncio.start_unix_server( self._accept_conn, path=str(self.ipc_path) ) self.logger.debug("Endpoint[%s]: server started", self.name)
Example #3
Source File: serve.py From async_dns with MIT License | 5 votes |
def start_server(handle, hostinfo): if is_path(hostinfo): try: os.remove(hostinfo) except FileNotFoundError: pass server = await asyncio.start_unix_server(handle, path=hostinfo) os.chmod(hostinfo, 0o666) return server host = Host(hostinfo) return await asyncio.start_server(handle, host=host.hostname, port=host.port)
Example #4
Source File: link.py From synapse with Apache License 2.0 | 5 votes |
def unixlisten(path, onlink): ''' Start an PF_UNIX server listening on the given path. ''' info = {'path': path, 'unix': True} async def onconn(reader, writer): link = await Link.anit(reader, writer, info=info) link.schedCoro(onlink(link)) return await asyncio.start_unix_server(onconn, path=path)
Example #5
Source File: ipc.py From trinity with MIT License | 5 votes |
def run(self) -> None: server = await asyncio.start_unix_server( connection_handler(self.rpc.execute), str(self.ipc_path), limit=MAXIMUM_REQUEST_BYTES, ) self.logger.info('IPC started at: %s', self.ipc_path.resolve()) try: await self.manager.wait_finished() finally: server.close() self.ipc_path.unlink()
Example #6
Source File: socket.py From DeepPavlov with Apache License 2.0 | 5 votes |
def __init__(self, model_config: Path, socket_type: str, port: Optional[int] = None, socket_file: Optional[Union[str, Path]] = None) -> None: """Initializes socket server. Args: model_config: Path to the config file. socket_type: Socket family. "TCP" for the AF_INET socket server, "UNIX" for UNIX Domain Socket server. port: Port number for the AF_INET address family. If parameter is not defined, the port number from the utils/settings/server_config.json is used. socket_file: Path to the file to which UNIX Domain Socket server connects. If parameter is not defined, the path from the utils/settings/server_config.json is used. Raises: ValueError: If ``socket_type`` parameter is neither "TCP" nor "UNIX". """ server_params = get_server_params(model_config) socket_type = socket_type or server_params['socket_type'] self._loop = asyncio.get_event_loop() if socket_type == 'TCP': host = server_params['host'] port = port or server_params['port'] self._launch_msg = f'{server_params["socket_launch_message"]} http://{host}:{port}' self._loop.create_task(asyncio.start_server(self._handle_client, host, port)) elif socket_type == 'UNIX': socket_file = socket_file or server_params['unix_socket_file'] socket_path = Path(socket_file).resolve() if socket_path.exists(): socket_path.unlink() self._launch_msg = f'{server_params["socket_launch_message"]} {socket_file}' self._loop.create_task(asyncio.start_unix_server(self._handle_client, socket_file)) else: raise ValueError(f'socket type "{socket_type}" is not supported') self._model = build_model(model_config) self._model_args_names = server_params['model_args_names']
Example #7
Source File: test_rpc.py From aiorpc with Do What The F*ck You Want To Public License | 5 votes |
def set_up_unix_server(): global loop, unix_server if not loop: loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) coro = asyncio.start_unix_server(serve, PATH) unix_server = loop.run_until_complete(coro)
Example #8
Source File: benchmark_aiorpc_unix.py From aiorpc with Do What The F*ck You Want To Public License | 5 votes |
def run_sum_server(): def sum(x, y): return x + y aiorpc.register('sum', sum) loop = uvloop.new_event_loop() asyncio.set_event_loop(loop) coro = asyncio.start_unix_server(aiorpc.serve, './benchmark.socket', loop=loop) loop.run_until_complete(coro) loop.run_forever()
Example #9
Source File: server.py From python-proxy with MIT License | 5 votes |
def start_server(self, args): handler = functools.partial(reuse_stream_handler if self.reuse else stream_handler, **vars(self), **args) if self.backward: return self.backward.start_server(handler) elif self.unix: return asyncio.start_unix_server(handler, path=self.bind) else: return asyncio.start_server(handler, host=self.host_name, port=self.port, reuse_port=args.get('ruport'))
Example #10
Source File: server.py From python-proxy with MIT License | 5 votes |
def client_run(self, args): async def handler(reader, writer): if self.uri.auth: try: assert self.uri.auth == (await reader.read_n(len(self.uri.auth))) except Exception: return await self.conn.put((reader, writer)) if self.uri.unix: return asyncio.start_unix_server(handler, path=self.uri.bind) else: return asyncio.start_server(handler, host=self.uri.host_name, port=self.uri.port, reuse_port=args.get('ruport'))
Example #11
Source File: responder.py From postfix-mta-sts-resolver with MIT License | 5 votes |
def start(self): def _spawn(reader, writer): def done_cb(task, fut): self._children.discard(task) task = self._loop.create_task(self.handler(reader, writer)) task.add_done_callback(partial(done_cb, task)) self._children.add(task) self._logger.debug("len(self._children) = %d", len(self._children)) if self._unix: self._server = await asyncio.start_unix_server(_spawn, path=self._path) if self._sockmode is not None: os.chmod(self._path, self._sockmode) else: if self._reuse_port: # pragma: no cover if sys.platform in ('win32', 'cygwin'): opts = { 'host': self._host, 'port': self._port, 'reuse_address': True, } elif os.name == 'posix': if sys.platform.startswith('freebsd'): sockopts = [ (socket.SOL_SOCKET, socket.SO_REUSEADDR, 1), (socket.SOL_SOCKET, 0x10000, 1), # SO_REUSEPORT_LB ] sock = await create_custom_socket(self._host, self._port, options=sockopts) opts = { 'sock': sock, } else: opts = { 'host': self._host, 'port': self._port, 'reuse_address': True, 'reuse_port': True, } self._server = await asyncio.start_server(_spawn, **opts)
Example #12
Source File: init.py From pop with Apache License 2.0 | 5 votes |
def pool(hub, num, name='Workers', callback=None, sock_dir=None): ''' Create a new local pool of process based workers :param num: The number of processes to add to this pool :param ref: The location on the hub to create the Workers dict used to store the worker pool, defaults to `hub.pop.proc.Workers` :param callback: The pop ref to call when the process communicates back ''' ret_ref = os.urandom(3).hex() + '.sock' ret_sock_path = os.path.join(sock_dir, ret_ref) if not hub.proc.Tracker: hub.proc.init.mk_tracker() workers = {} if callback: await asyncio.start_unix_server( hub.proc.init.ret_work(callback), path=ret_sock_path) for ind in range(num): hub.proc.init.mk_proc(ind, workers, ret_ref, sock_dir) w_iter = itertools.cycle(workers) hub.proc.Workers[name] = workers hub.proc.WorkersIter[name] = w_iter hub.proc.WorkersTrack[name] = { 'subs': [], 'ret_ref': ret_ref, 'sock_dir': sock_dir} up = set() while True: for ind in workers: if os.path.exists(workers[ind]['path']): up.add(ind) if len(up) == num: break await asyncio.sleep(0.01) # TODO: This seems to be spawning extra procs, this should be fixed #asyncio.ensure_future(hub.proc.init.maintain(name))
Example #13
Source File: worker.py From pop with Apache License 2.0 | 5 votes |
def server(hub): ''' Start the unix socket server to receive commands ''' await asyncio.start_unix_server( hub.proc.worker.work, path=hub.proc.SOCK_PATH)
Example #14
Source File: manhole.py From mautrix-python with Mozilla Public License 2.0 | 5 votes |
def start_manhole(path: str, banner: str = "", namespace: Optional[Dict[str, Any]] = None, loop: asyncio.AbstractEventLoop = None, whitelist: Set[int] = None, ) -> Tuple[asyncio.AbstractServer, Callable[[], None]]: """ Starts a manhole server on a given UNIX address. Args: path: The path to create the UNIX socket at. banner: The banner to show when clients connect. namespace: The globals to provide to connected clients. loop: The asyncio event loop to use. whitelist: List of user IDs to allow connecting. """ if not SO_PEERCRED: raise ValueError("SO_PEERCRED is not supported on this platform") loop = loop or asyncio.get_event_loop() factory = InterpreterFactory(namespace=namespace, banner=banner, interpreter_class=AsyncInterpreter, loop=loop, whitelist=whitelist) server = await asyncio.start_unix_server(factory, path=path, loop=loop) os.chmod(path, 0o666) def stop(): for client in factory.clients: client.close() server.close() return server, stop
Example #15
Source File: mtprotoproxy.py From mtprotoproxy with MIT License | 5 votes |
def create_servers(loop): servers = [] reuse_port = hasattr(socket, "SO_REUSEPORT") has_unix = hasattr(socket, "AF_UNIX") if config.LISTEN_ADDR_IPV4: task = asyncio.start_server(handle_client_wrapper, config.LISTEN_ADDR_IPV4, config.PORT, limit=get_to_tg_bufsize(), reuse_port=reuse_port) servers.append(loop.run_until_complete(task)) if config.LISTEN_ADDR_IPV6 and socket.has_ipv6: task = asyncio.start_server(handle_client_wrapper, config.LISTEN_ADDR_IPV6, config.PORT, limit=get_to_tg_bufsize(), reuse_port=reuse_port) servers.append(loop.run_until_complete(task)) if config.LISTEN_UNIX_SOCK and has_unix: remove_unix_socket(config.LISTEN_UNIX_SOCK) task = asyncio.start_unix_server(handle_client_wrapper, config.LISTEN_UNIX_SOCK, limit=get_to_tg_bufsize()) servers.append(loop.run_until_complete(task)) os.chmod(config.LISTEN_UNIX_SOCK, 0o666) if config.METRICS_PORT is not None: if config.METRICS_LISTEN_ADDR_IPV4: task = asyncio.start_server(handle_metrics, config.METRICS_LISTEN_ADDR_IPV4, config.METRICS_PORT) servers.append(loop.run_until_complete(task)) if config.METRICS_LISTEN_ADDR_IPV6 and socket.has_ipv6: task = asyncio.start_server(handle_metrics, config.METRICS_LISTEN_ADDR_IPV6, config.METRICS_PORT) servers.append(loop.run_until_complete(task)) return servers
Example #16
Source File: test_server.py From android_universal with MIT License | 4 votes |
def test_start_unix_server_1(self): HELLO_MSG = b'1' * 1024 * 5 + b'\n' started = threading.Event() def client(sock, addr): sock.settimeout(2) started.wait(5) sock.connect(addr) sock.send(HELLO_MSG) sock.recv_all(1) sock.close() async def serve(reader, writer): await reader.readline() main_task.cancel() writer.write(b'1') writer.close() await writer.wait_closed() async def main(srv): async with srv: self.assertFalse(srv.is_serving()) await srv.start_serving() self.assertTrue(srv.is_serving()) started.set() await srv.serve_forever() with test_utils.unix_socket_path() as addr: srv = self.loop.run_until_complete(asyncio.start_unix_server( serve, addr, loop=self.loop, start_serving=False)) main_task = self.loop.create_task(main(srv)) with self.assertRaises(asyncio.CancelledError): with self.unix_client(lambda sock: client(sock, addr)): self.loop.run_until_complete(main_task) self.assertEqual(srv.sockets, []) self.assertIsNone(srv._sockets) self.assertIsNone(srv._waiters) self.assertFalse(srv.is_serving()) with self.assertRaisesRegex(RuntimeError, r'is closed'): self.loop.run_until_complete(srv.serve_forever())
Example #17
Source File: socket.py From thriftpy2 with MIT License | 4 votes |
def __init__(self, host=None, port=None, unix_socket=None, socket_family=socket.AF_INET, client_timeout=3000, backlog=128, ssl_context=None, certfile=None, keyfile=None, ciphers=RESTRICTED_SERVER_CIPHERS): """Initialize a TServerSocket TSocket can be initialized in 2 ways: * host + port. can configure to use AF_INET/AF_INET6 * unix_socket @param host(str) The host to connect to @param port(int) The (TCP) port to connect to @param unix_socket(str) The filename of a unix socket to connect to @param socket_family(str) socket.AF_INET or socket.AF_INET6. only take effect when using host/port @param client_timeout client socket timeout @param backlog backlog for server socket @param certfile(str) The server cert pem filename @param keyfile(str) The server cert key filename @param ciphers(list<str>) The cipher suites to allow @param ssl_context(SSLContext) Customize the SSLContext, can be used to persist SSLContext object. Caution it's easy to get wrong, only use if you know what you're doing. """ if unix_socket: self.unix_socket = unix_socket self.host = None self.port = None self.sock_factory = asyncio.start_unix_server else: self.unix_socket = None self.host = host self.port = port self.sock_factory = asyncio.start_server self.socket_family = socket_family self.client_timeout = client_timeout / 1000 if client_timeout else None self.backlog = backlog if ssl_context: self.ssl_context = ssl_context elif certfile: if not os.access(certfile, os.R_OK): raise IOError('No such certfile found: %s' % certfile) self.ssl_context = create_thriftpy_context(server_side=True, ciphers=ciphers) self.ssl_context.load_cert_chain(certfile, keyfile=keyfile) else: self.ssl_context = None