Python asyncio.start_server() Examples
The following are 30
code examples of asyncio.start_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: test_http.py From heralding with GNU General Public License v3.0 | 7 votes |
def test_connection(self): """ Tests if the capability is up, and sending HTTP 401 (Unauthorized) headers. """ def http_request(): client = httpclient.HTTPConnection('127.0.0.1', 8888) client.request('GET', '/') response = client.getresponse() self.assertEqual(response.status, 401) options = {'enabled': 'True', 'port': 8888, 'users': {'test': 'test'}} http_cap = http.Http(options, self.loop) server_coro = asyncio.start_server( http_cap.handle_session, '0.0.0.0', 8888, loop=self.loop) self.server = self.loop.run_until_complete(server_coro) http_task = self.loop.run_in_executor(None, http_request) self.loop.run_until_complete(http_task)
Example #2
Source File: norm_server.py From aiostream with GNU General Public License v3.0 | 6 votes |
def run_server(bind='127.0.0.1', port=8888): # Start the server loop = asyncio.get_event_loop() coro = asyncio.start_server(euclidean_norm_handler, bind, port) server = loop.run_until_complete(coro) # Serve requests until Ctrl+C is pressed print('Serving on {}'.format(server.sockets[0].getsockname())) try: loop.run_forever() except KeyboardInterrupt: pass # Close the server server.close() loop.run_until_complete(server.wait_closed()) loop.close() # Main execution
Example #3
Source File: client.py From wstan with MIT License | 6 votes |
def main(): if config.tfo: loop.set_exception_handler(silent_tpo_timeout_err_handler) try: server = loop.run_until_complete( asyncio.start_server(dispatch_proxy, 'localhost', config.port)) except OSError: die('wstan client failed to bind on localhost:%d' % config.port) print('wstan client -- SOCKS5/HTTP(S) server listening on localhost:%d' % config.port) try: loop.run_forever() except KeyboardInterrupt: pass finally: server.close() loop.close()
Example #4
Source File: command_server.py From PythonTwitchBotFramework with MIT License | 6 votes |
def start_command_server(): if not ENABLED: return stop_command_server() print(f'starting command server (view host / port in config file)') try: # noinspection PyTypeChecker add_task(COMMAND_SERVER_TASK_ID, start_server(handle_client, HOST, PORT)) except Exception as e: print(f"\n------COMMAND SERVER------\nfailed to bind/create command server\n" f"this does not affect the bot, but it does mean that the command console will not work/be usable\n" f"if this error happens a lot, command server can be disabled in the config.json in the bot's configs folder\n" f'\nERROR INFO: {e}\n' f'EXTENDED INFO: \n{format_exc()}\n\n' f'------COMMAND SERVER------\n')
Example #5
Source File: test_postgresql.py From heralding with GNU General Public License v3.0 | 6 votes |
def test_invalid_login(self): """Tests if postgres server responds correctly to a invalid login attempt.""" def postgresql_login(): try: psycopg2.connect("postgres://scott:tiger@0.0.0.0:2504/") except psycopg2.OperationalError as e: return e return None options = {'enabled': 'True', 'port': 2504} postgresql_cap = postgresql.PostgreSQL(options, self.loop) server_coro = asyncio.start_server( postgresql_cap.handle_session, '0.0.0.0', 2504, loop=self.loop) self.server = self.loop.run_until_complete(server_coro) postgresql_task = self.loop.run_in_executor(None, postgresql_login) login_exception = self.loop.run_until_complete(postgresql_task) self.assertIsInstance(login_exception, psycopg2.OperationalError) self.assertEqual( str(login_exception), 'FATAL: password authentication failed for user "scott"\n')
Example #6
Source File: tcp_charfinder.py From notebooks with MIT License | 6 votes |
def main(address='127.0.0.1', port=2323): # <1> port = int(port) loop = asyncio.get_event_loop() server_coro = asyncio.start_server(handle_queries, address, port, loop=loop) # <2> server = loop.run_until_complete(server_coro) # <3> host = server.sockets[0].getsockname() # <4> print('Serving on {}. Hit CTRL-C to stop.'.format(host)) # <5> try: loop.run_forever() # <6> except KeyboardInterrupt: # CTRL+C pressed pass print('Server shutting down.') server.close() # <7> loop.run_until_complete(server.wait_closed()) # <8> loop.close() # <9>
Example #7
Source File: tcp_charfinder.py From notebooks with MIT License | 6 votes |
def main(address='127.0.0.1', port=8888): port = int(port) loop = asyncio.get_event_loop() coro = asyncio.start_server(handle_queries, address, port, loop=loop) server = loop.run_until_complete(coro) host = server.sockets[0].getsockname() print('Serving on {}. Hit CTRL-C to stop.'.format(host)) try: loop.run_forever() except KeyboardInterrupt: # CTRL+C pressed pass server.close() loop.run_until_complete(server.wait_closed()) loop.close()
Example #8
Source File: tcp_charfinder.py From notebooks with MIT License | 6 votes |
def main(address='127.0.0.1', port=2323): # <1> port = int(port) loop = asyncio.get_event_loop() server_coro = asyncio.start_server(handle_queries, address, port, loop=loop) # <2> server = loop.run_until_complete(server_coro) # <3> host = server.sockets[0].getsockname() # <4> print('Serving on {}. Hit CTRL-C to stop.'.format(host)) # <5> try: loop.run_forever() # <6> except KeyboardInterrupt: # CTRL+C pressed pass print('Server shutting down.') server.close() # <7> loop.run_until_complete(server.wait_closed()) # <8> loop.close() # <9>
Example #9
Source File: test_protocol.py From aiosmtplib with MIT License | 6 votes |
def test_error_on_readline_with_partial_line(event_loop, bind_address, hostname): partial_response = b"499 incomplete response\\" async def client_connected(reader, writer): writer.write(partial_response) writer.write_eof() await writer.drain() server = await asyncio.start_server( client_connected, host=bind_address, port=0, family=socket.AF_INET ) server_port = server.sockets[0].getsockname()[1] connect_future = event_loop.create_connection( SMTPProtocol, host=hostname, port=server_port ) _, protocol = await asyncio.wait_for(connect_future, timeout=1.0) with pytest.raises(SMTPServerDisconnected): await protocol.read_response(timeout=1.0) server.close() await server.wait_closed()
Example #10
Source File: network.py From smarthome with GNU General Public License v3.0 | 6 votes |
def start(self): """ Start the server socket :return: False if an error prevented us from launching a connection thread. True if a connection thread has been started. :rtype: bool """ if self._is_listening: return False try: self.logger.info("Starting up TCP server socket {}".format(self.__our_socket)) self.__loop = asyncio.new_event_loop() asyncio.set_event_loop(self.__loop) self.__coroutine = asyncio.start_server(self.__handle_connection, self._interfaceip, self._port) self.__server = self.__loop.run_until_complete(self.__coroutine) self.__listening_thread = threading.Thread(target=self.__listening_thread_worker, name='TCP_Server_{}'.format(self.name)) self.__listening_thread.daemon = True self.__listening_thread.start() except: return False return True
Example #11
Source File: test_timeouts.py From aiosmtplib with MIT License | 6 votes |
def test_protocol_timeout_on_starttls( event_loop, bind_address, hostname, client_tls_context ): async def client_connected(reader, writer): await asyncio.sleep(1.0) server = await asyncio.start_server( client_connected, host=bind_address, port=0, family=socket.AF_INET ) server_port = server.sockets[0].getsockname()[1] connect_future = event_loop.create_connection( SMTPProtocol, host=hostname, port=server_port ) _, protocol = await asyncio.wait_for(connect_future, timeout=1.0) with pytest.raises(SMTPTimeoutError): # STARTTLS timeout must be > 0 await protocol.start_tls(client_tls_context, timeout=0.00001) server.close() await server.wait_closed()
Example #12
Source File: test_connectrum.py From spruned with MIT License | 6 votes |
def _setup_electrum_server(self, server_info): async def methods(r, w): responses = { 'server.version': 'mock 1.2 1.2', 'blockchain.scripthash.listunspent': 'cafebabe', 'something.subscribe': 'babe', 'server.ping': True } while 1: data = await r.read(1024) if not data: w.close() break else: d = json.loads(data.strip().decode()) command = d['method'] response = {'result': responses[command], 'id': d['id']} res = json.dumps(response) + '\n' w.write(res.encode()) await w.drain() host = server_info.hostname coro = asyncio.start_server(methods, host=host, port=50001, loop=self.loop) return coro
Example #13
Source File: app.py From FeelUOwn with GNU General Public License v3.0 | 5 votes |
def run_app(app): loop = asyncio.get_event_loop() if app.mode & (App.DaemonMode | App.GuiMode): loop.call_later(10, partial(loop.create_task, app.version_mgr.check_release())) if app.mode & App.DaemonMode: if sys.platform.lower() == 'darwin': try: from .global_hotkey_mac import MacGlobalHotkeyManager except ImportError as e: logger.warning("Can't start mac hotkey listener: %s", str(e)) else: mac_global_hotkey_mgr = MacGlobalHotkeyManager() mac_global_hotkey_mgr.start() if sys.platform.lower() == 'linux': from feeluown.linux import run_mpris2_server run_mpris2_server(app) loop.create_task(app.server.run(app.get_listen_addr())) client_connected_cb = PubsubHandlerV1(app.pubsub_gateway).handle loop.create_task(asyncio.start_server( client_connected_cb, host=app.get_listen_addr(), port=23334, loop=loop)) try: if not (app.config.MODE & (App.GuiMode | App.DaemonMode)): logger.warning('Fuo running with no daemon and no window') loop.run_forever() except KeyboardInterrupt: # NOTE: gracefully shutdown? pass finally: _shutdown_app(app) loop.stop() loop.close()
Example #14
Source File: server.py From python-proxy with MIT License | 5 votes |
def start_server(self, handler): for _ in range(self.count): asyncio.ensure_future(self.server_run(handler)) return self
Example #15
Source File: test_connection.py From aioftp with Apache License 2.0 | 5 votes |
def test_pasv_connection_busy_port2(pair_factory, Server, unused_tcp_port_factory, expect_codes_in_exception): ports = [unused_tcp_port_factory()] s = Server(data_ports=ports) async with pair_factory(None, s, do_quit=False) as pair: conflicting_server = await asyncio.start_server( lambda r, w: w.close(), host=pair.server.server_host, port=ports[0], ) with expect_codes_in_exception("421"): await pair.client.get_passive_connection() conflicting_server.close() await conflicting_server.wait_closed()
Example #16
Source File: server.py From ProxyBroker with Apache License 2.0 | 5 votes |
def start(self): srv = asyncio.start_server( self._accept, host=self.host, port=self.port, backlog=self._backlog, loop=self._loop, ) self._server = self._loop.run_until_complete(srv) log.info( 'Listening established on {0}'.format( self._server.sockets[0].getsockname() ) )
Example #17
Source File: test_simple.py From aiofiles with Apache License 2.0 | 5 votes |
def test_serve_small_bin_file_sync(event_loop, tmpdir, unused_tcp_port): """Fire up a small simple file server, and fetch a file. The file is read into memory synchronously, so this test doesn't actually test anything except the general test concept. """ # First we'll write a small file. filename = "test.bin" file_content = b"0123456789" file = tmpdir.join(filename) file.write_binary(file_content) async def serve_file(reader, writer): full_filename = str(file) with open(full_filename, "rb") as f: writer.write(f.read()) writer.close() server = await asyncio.start_server( serve_file, port=unused_tcp_port, loop=event_loop ) reader, _ = await asyncio.open_connection( host="localhost", port=unused_tcp_port, loop=event_loop ) payload = await reader.read() assert payload == file_content server.close() await server.wait_closed()
Example #18
Source File: test_simple.py From aiofiles with Apache License 2.0 | 5 votes |
def test_serve_small_bin_file(event_loop, tmpdir, unused_tcp_port): """Fire up a small simple file server, and fetch a file.""" # First we'll write a small file. filename = "test.bin" file_content = b"0123456789" file = tmpdir.join(filename) file.write_binary(file_content) async def serve_file(reader, writer): full_filename = str(file) f = await threadpool.open(full_filename, mode="rb") writer.write((await f.read())) await f.close() writer.close() server = await asyncio.start_server( serve_file, port=unused_tcp_port, loop=event_loop ) reader, _ = await asyncio.open_connection( host="localhost", port=unused_tcp_port, loop=event_loop ) payload = await reader.read() assert payload == file_content server.close() await server.wait_closed()
Example #19
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 #20
Source File: test_protocol.py From aiosmtplib with MIT License | 5 votes |
def test_protocol_data_received_called_twice( event_loop, bind_address, hostname, monkeypatch ): async def client_connected(reader, writer): await reader.read(1000) writer.write(b"220 Hi\r\n") await writer.drain() await asyncio.sleep(0) writer.write(b"221 Hi again!\r\n") await writer.drain() server = await asyncio.start_server( client_connected, host=bind_address, port=0, family=socket.AF_INET ) server_port = server.sockets[0].getsockname()[1] connect_future = event_loop.create_connection( SMTPProtocol, host=hostname, port=server_port ) _, protocol = await asyncio.wait_for(connect_future, timeout=1.0) response = await protocol.execute_command(b"TEST\n", timeout=1.0) assert response.code == 220 assert response.message == "Hi" server.close() await server.wait_closed()
Example #21
Source File: test_connection.py From aioftp with Apache License 2.0 | 5 votes |
def test_pasv_connection_busy_port(pair_factory, Server, unused_tcp_port_factory): ports = [unused_tcp_port_factory(), unused_tcp_port_factory()] async with pair_factory(None, Server(data_ports=ports)) as pair: conflicting_server = await asyncio.start_server( lambda r, w: w.close(), host=pair.server.server_host, port=ports[0], ) r, w = await pair.client.get_passive_connection() host, port, *_ = w.transport.get_extra_info("peername") assert port == ports[1] assert pair.server.available_data_ports.qsize() == 1 conflicting_server.close() await conflicting_server.wait_closed()
Example #22
Source File: conftest.py From aioftp with Apache License 2.0 | 5 votes |
def socks(request, unused_tcp_port): handler = functools.partial( socks_server_handler, allowed_versions={5}, username="foo", password="bar", ) Socks = collections.namedtuple("Socks", "host port server") host, family = request.param port = unused_tcp_port server = await asyncio.start_server(handler, host=host, port=port, family=family) yield Socks(host, port, server) server.close() await server.wait_closed()
Example #23
Source File: server.py From aioftp with Apache License 2.0 | 5 votes |
def _start_passive_server(self, connection, handler_callback): if self.available_data_ports is not None: viewed_ports = set() while True: try: priority, port = self.available_data_ports.get_nowait() if port in viewed_ports: raise errors.NoAvailablePort viewed_ports.add(port) passive_server = await asyncio.start_server( handler_callback, connection.server_host, port, ssl=self.ssl, **self._start_server_extra_arguments, ) connection.passive_server_port = port break except asyncio.QueueEmpty: raise errors.NoAvailablePort except OSError as err: self.available_data_ports.put_nowait((priority + 1, port)) if err.errno != errno.EADDRINUSE: raise else: passive_server = await asyncio.start_server( handler_callback, connection.server_host, connection.passive_server_port, ssl=self.ssl, **self._start_server_extra_arguments, ) return passive_server
Example #24
Source File: server.py From aioftp with Apache License 2.0 | 5 votes |
def start(self, host=None, port=0, **kwargs): """ :py:func:`asyncio.coroutine` Start server. :param host: ip address to bind for listening. :type host: :py:class:`str` :param port: port number to bind for listening. :type port: :py:class:`int` :param kwargs: keyword arguments, they passed to :py:func:`asyncio.start_server` """ self._start_server_extra_arguments = kwargs self.connections = {} self.server_host = host self.server_port = port self.server = await asyncio.start_server( self.dispatcher, host, port, ssl=self.ssl, **self._start_server_extra_arguments, ) for sock in self.server.sockets: if sock.family in (socket.AF_INET, socket.AF_INET6): host, port, *_ = sock.getsockname() if not self.server_port: self.server_port = port if not self.server_host: self.server_host = host logger.info("serving on %s:%s", host, port)
Example #25
Source File: serverthread.py From Amipy with MIT License | 5 votes |
def serve(self): coro = asyncio.start_server(self._handle_request,self.host,self.port,loop=self.loop) server = self.loop.run_until_complete(coro) addr,port = server.sockets[0].getsockname() print(f'* Spider server serving on {addr}:{port}.') print('* Press Ctrl+C to stop the crawling.\n') try: self.loop.run_forever() except (KeyboardInterrupt,StopAsyncIteration): print('Shutting down spider server.') server.close() self.loop.run_until_complete(server.wait_closed()) self.loop.close()
Example #26
Source File: setup_nodes.py From chia-blockchain with Apache License 2.0 | 5 votes |
def setup_introducer(port, dic={}): net_config = load_config(root_path, "config.yaml") ping_interval = net_config.get("ping_interval") network_id = net_config.get("network_id") config = load_config(root_path, "config.yaml", "introducer") introducer = Introducer( config["max_peers_to_send"], config["recent_peer_threshold"] ) assert ping_interval is not None assert network_id is not None server = ChiaServer( port, introducer, NodeType.INTRODUCER, ping_interval, network_id, bt.root_path, config, f"introducer_server_{port}", ) _ = await start_server(server) yield (introducer, server) _.close() server.close_all() await server.await_closed()
Example #27
Source File: setup_nodes.py From chia-blockchain with Apache License 2.0 | 5 votes |
def setup_farmer(port, dic={}): print("root path", root_path) config = load_config(root_path, "config.yaml", "farmer") config_pool = load_config(root_path, "config.yaml", "pool") test_constants_copy = test_constants.copy() for k in dic.keys(): test_constants_copy[k] = dic[k] net_config = load_config(root_path, "config.yaml") ping_interval = net_config.get("ping_interval") network_id = net_config.get("network_id") config["xch_target_puzzle_hash"] = bt.fee_target.hex() config["pool_public_keys"] = [ bytes(epk.get_public_key()).hex() for epk in bt.keychain.get_all_public_keys() ] config_pool["xch_target_puzzle_hash"] = bt.fee_target.hex() farmer = Farmer(config, config_pool, bt.keychain, test_constants_copy) assert ping_interval is not None assert network_id is not None server = ChiaServer( port, farmer, NodeType.FARMER, ping_interval, network_id, root_path, config, f"farmer_server_{port}", ) farmer.set_server(server) _ = await start_server(server, farmer._on_connect) yield (farmer, server) _.close() server.close_all() await server.await_closed()
Example #28
Source File: PSHandler.py From Harness with MIT License | 5 votes |
def run_module(self): self.loop = asyncio.new_event_loop() asyncio.set_event_loop(self.loop) if self.options.SSL: sc = ssl.SSLContext(ssl.PROTOCOL_SSLv23) sc.load_cert_chain(self.options.CERT_PATH, self.options.KEY_PATH) self.server = self.loop.run_until_complete(asyncio.start_server(self.client_connected_handler, self.options.IP, self.options.PORT, ssl=sc)) else: self.server = self.loop.run_until_complete(asyncio.start_server(self.client_connected_handler, self.options.IP, self.options.PORT)) asyncio.async(self.cancel_tasks()) # send server and call stop if not isrunning() try: self.loop.run_forever() except KeyboardInterrupt: pass finally: self.stopper.set() self.loop.run_until_complete(self.cancel_tasks()) self.server.close() self.loop.run_until_complete(server.wait_closed()) self.loop.stop() self.loop.close()
Example #29
Source File: sockslistener.py From rsp with MIT License | 5 votes |
def start(self): def _spawn(reader, writer): def task_cb(task, fut): self._children.discard(task) task = self._loop.create_task(self.handler(reader, writer)) self._children.add(task) task.add_done_callback(partial(task_cb, task)) self._server = await asyncio.start_server(_spawn, self._listen_address, self._listen_port) self._logger.info("SOCKS5 server listening on %s:%d", self._listen_address, self._listen_port)
Example #30
Source File: transparentlistener.py From rsp with MIT License | 5 votes |
def start(self): def _spawn(reader, writer): def task_cb(task, fut): self._children.discard(task) task = self._loop.create_task(self.handler(reader, writer)) self._children.add(task) task.add_done_callback(partial(task_cb, task)) self._server = await asyncio.start_server(_spawn, self._listen_address, self._listen_port) self._logger.info("Transparent Proxy server listening on %s:%d", self._listen_address, self._listen_port)