Python asyncio.open_connection() Examples

The following are 30 code examples of asyncio.open_connection(). 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_bad_request.py    From sanic with MIT License 6 votes vote down vote up
def test_bad_request_response(app):
    lines = []

    @app.listener("after_server_start")
    async def _request(sanic, loop):
        connect = asyncio.open_connection("127.0.0.1", 42101)
        reader, writer = await connect
        writer.write(b"not http")
        while True:
            line = await reader.readline()
            if not line:
                break
            lines.append(line)
        app.stop()

    app.run(host="127.0.0.1", port=42101, debug=False)
    assert lines[0] == b"HTTP/1.1 400 Bad Request\r\n"
    assert b"Bad Request" in lines[-1] 
Example #2
Source File: mtprotoproxy.py    From mtprotoproxy with MIT License 6 votes vote down vote up
def open_tg_connection(self, host, port, init_func=None):
        task = asyncio.open_connection(host, port, limit=get_to_clt_bufsize())
        reader_tgt, writer_tgt = await asyncio.wait_for(task, timeout=config.TG_CONNECT_TIMEOUT)

        set_keepalive(writer_tgt.get_extra_info("socket"))
        set_bufsizes(writer_tgt.get_extra_info("socket"), get_to_clt_bufsize(), get_to_tg_bufsize())

        if init_func:
            return await asyncio.wait_for(init_func(host, port, reader_tgt, writer_tgt),
                                          timeout=config.TG_CONNECT_TIMEOUT)
        return reader_tgt, writer_tgt 
Example #3
Source File: client.py    From aioftp with Apache License 2.0 6 votes vote down vote up
def __init__(self, *, socket_timeout=None,
                 read_speed_limit=None, write_speed_limit=None,
                 path_timeout=None, path_io_factory=pathio.PathIO,
                 encoding="utf-8", ssl=None, parse_list_line_custom=None,
                 **siosocks_asyncio_kwargs):
        self.socket_timeout = socket_timeout
        self.throttle = StreamThrottle.from_limits(
            read_speed_limit,
            write_speed_limit,
        )
        self.path_timeout = path_timeout
        self.path_io = path_io_factory(timeout=path_timeout)
        self.encoding = encoding
        self.stream = None
        self.ssl = ssl
        self.parse_list_line_custom = parse_list_line_custom
        self._open_connection = partial(open_connection, ssl=self.ssl,
                                        **siosocks_asyncio_kwargs) 
Example #4
Source File: 13_10_async_client.py    From Python-Journey-from-Novice-to-Expert with MIT License 6 votes vote down vote up
def remote_sort():
    reader, writer = yield from asyncio.open_connection('127.0.0.1', 2015)
    print("Generating random list...")
    numbers = [random.randrange(10000) for r in range(10000)]
    data = json.dumps(numbers).encode()
    print("List Generated, Sending data")
    writer.write(len(data).to_bytes(8, 'big'))
    writer.write(data)

    print("Waiting for data...")
    data = yield from reader.readexactly(len(data))
    print("Received data")
    sorted_values = json.loads(data.decode())
    print(sorted_values)
    print('\n')
    writer.close() 
Example #5
Source File: tcp.py    From msldap with MIT License 6 votes vote down vote up
def run(self):
		try:
			self.in_queue = asyncio.Queue()
			self.out_queue = asyncio.Queue()
			self.reader, self.writer = await asyncio.wait_for(
				asyncio.open_connection(
					self.target.serverip if self.target.serverip is not None else self.target.host, 
					self.target.port, 
					ssl=self.target.get_ssl_context()
					),
				timeout = self.target.timeout
			)

			self.handle_in_task = asyncio.create_task(self.handle_in_q())
			self.handle_out_task = asyncio.create_task(self.handle_out_q())
			return True, None
		except Exception as e:
			return False, e 
Example #6
Source File: test_qeventloop.py    From asyncqt with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def test_reader_writer_echo(loop, sock_pair):
    """Verify readers and writers can send data to each other."""
    c_sock, s_sock = sock_pair

    @asyncio.coroutine
    def mycoro():
        c_reader, c_writer = yield from asyncio.open_connection(sock=c_sock)
        s_reader, s_writer = yield from asyncio.open_connection(sock=s_sock)

        data = b'Echo... Echo... Echo...'
        s_writer.write(data)
        yield from s_writer.drain()
        read_data = yield from c_reader.readexactly(len(data))
        assert data == read_data
        s_writer.close()

    loop.run_until_complete(asyncio.wait_for(mycoro(), timeout=1.0)) 
Example #7
Source File: 08_echo_server.py    From Python-Journey-from-Novice-to-Expert with MIT License 6 votes vote down vote up
def create_connection(repetitions):
    reader, writer = await asyncio.open_connection(
        host=HOST, port=PORT)

    start_time = float((await reader.readline()))

    writer.write(repetitions.encode() + b'\n')
    await writer.drain()

    async for line in reader:
        # Sleeping a little to emulate processing time and make
        # it easier to add more simultaneous clients
        await asyncio.sleep(1)

        printer(start_time, 'Got line: ', line.decode(),
                end='')

    writer.close() 
Example #8
Source File: master.py    From pyquarkchain with MIT License 6 votes vote down vote up
def __connect(self, host, port):
        """ Retries until success """
        Logger.info("Trying to connect {}:{}".format(host, port))
        while True:
            try:
                reader, writer = await asyncio.open_connection(
                    host, port, loop=self.loop
                )
                break
            except Exception as e:
                Logger.info("Failed to connect {} {}: {}".format(host, port, e))
                await asyncio.sleep(
                    self.env.cluster_config.MASTER.MASTER_TO_SLAVE_CONNECT_RETRY_DELAY
                )
        Logger.info("Connected to {}:{}".format(host, port))
        return reader, writer 
Example #9
Source File: console_script.py    From ptadapter with GNU General Public License v3.0 6 votes vote down vote up
def handle_ext_server_connection(
        upstream_host: str,
        upstream_port: int,
        reader: asyncio.StreamReader,
        writer: asyncio.StreamWriter,
        info: adapters.ExtOrPortClientConnection,
) -> None:
    handler_logger.info('Connection received from %r', info)
    async with contexts.log_unhandled_exc(handler_logger), \
               contexts.aclosing_multiple_writers(writer) as writers:
        try:
            ureader, uwriter = await asyncio.open_connection(
                upstream_host, upstream_port)
        except OSError as e:
            handler_logger.warning(
                'Error while connecting to upstream: %r', e)
            return
        writers.add(writer)
        try:
            await relays.relay(reader, writer, ureader, uwriter)
        except OSError as e:
            handler_logger.warning('Connection from %r caught %r', info, e) 
Example #10
Source File: server.py    From wstan with MIT License 6 votes vote down vote up
def connectTarget(self, addr, port, data):
        logging.info('requested %s <--> %s:%s' % (self.peer, addr, port))
        try:
            reader, writer = yield from open_connection(addr, port)
        except (ConnectionError, OSError, TimeoutError) as e:
            logging.info("can't connect to %s:%s (from %s)" % (addr, port, self.peer))
            return self.resetTunnel("can't connect to %s:%s" % (addr, port), str(e))
        self.setProxy(reader, writer)
        if data:
            writer.write(data)
        if self._dataToTarget:
            writer.write(self._dataToTarget)
            self._dataToTarget.clear()
        self.connectTargetTask = None

    # next 2 overrides deal with a implicit state which exists only in wstan server: CONNECTING
    # data received during CONNECTING will be sent after connected
    # IDLE --onConnect--> CONNECTING --connectTarget--> USING
    # CONNECTING --RST-received-and-RST-sent--> IDLE
    # CONNECTING --RST-sent--> RESETTING --RST-received--> IDLE 
Example #11
Source File: streammanagerclient.py    From aws-greengrass-core-sdk-python with Apache License 2.0 6 votes vote down vote up
def __connect(self):
        self.__check_closed()
        if self.connected:
            return
        try:
            self.logger.debug("Opening connection to %s:%d", self.host, self.port)
            future = asyncio.open_connection(self.host, self.port, loop=self.__loop)
            self.__reader, self.__writer = await asyncio.wait_for(
                future, timeout=self.connect_timeout, loop=self.__loop
            )

            await asyncio.wait_for(self.__connect_request_response(), timeout=self.request_timeout, loop=self.__loop)

            self.logger.debug("Socket connected successfully. Starting read loop.")
            self.connected = True
            self.__loop.create_task(self.__read_loop())
        except ConnectionError as e:
            self.logger.error("Connection error while connecting to server: %s", e)
            raise 
Example #12
Source File: network_down_detector.py    From moler with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def tcp_connection(address):
    """Async generator reading from tcp network transport layer"""
    logger = logging.getLogger('asyncio.tcp-connection')
    logger.debug('... connecting to tcp://{}:{}'.format(*address))
    reader, writer = await asyncio.open_connection(*address)
    try:
        while True:
            data = await reader.read(128)
            if data:
                logger.debug('<<< {!r}'.format(data))
                yield data
            else:
                break
    finally:
        logger.debug('... closing')
        writer.close()


# ============================================================================== 
Example #13
Source File: knx.py    From knx with MIT License 6 votes vote down vote up
def bus_monitor(receiver,
                      host='localhost',
                      port=6720,
                      decoder=telegram_decoder):
    """ creates a connection to host:port and starts to receive telegrams

    :param receiver: a coroutine or instance of a class that has a `send`
                     method which takes one argument to receive a telegram.
    :param host: hostname to which to connect to
    :param port: port to which to connect to
    :param decoder: optional alternative decoder to transform binary data into
                    telegrams

    received telegrams will be sent to the receiver.
    """
    reader, writer = await open_connection(host, port)
    await listen(reader, receiver, decoder)
    writer.close() 
Example #14
Source File: sort_client.py    From Python-3-Object-Oriented-Programming-Third-Edition with MIT License 6 votes vote down vote up
def remote_sort():
    reader, writer = await asyncio.open_connection("127.0.0.1", 2015)
    print("Generating random list...")
    numbers = [random.randrange(10000) for r in range(10000)]
    data = json.dumps(numbers).encode()
    print("List Generated, Sending data")
    writer.write(len(data).to_bytes(8, "big"))
    writer.write(data)

    print("Waiting for data...")
    data = await reader.readexactly(len(data))
    print("Received data")
    sorted_values = json.loads(data.decode())
    print(sorted_values)
    print("\n")
    writer.close() 
Example #15
Source File: tcp_client.py    From agents-aea with Apache License 2.0 5 votes vote down vote up
def setup(self):
        """Set the connection up."""
        self._reader, self._writer = await asyncio.open_connection(self.host, self.port)
        address_bytes = self.address.encode("utf-8")
        await self._send(self._writer, address_bytes) 
Example #16
Source File: wallet.py    From wallets with Apache License 2.0 5 votes vote down vote up
def ledger_sim_proxy():
    """
    Return an async proxy to the ledger sim instance running on 9868.
    """
    from chiasim.clients import ledger_sim
    from chiasim.remote.client import request_response_proxy

    reader, writer = await asyncio.open_connection(host="localhost", port=9868)
    proxy = request_response_proxy(reader, writer, ledger_sim.REMOTE_SIGNATURES)
    return proxy 
Example #17
Source File: connection.py    From aredis with MIT License 5 votes vote down vote up
def _connect(self):
        reader, writer = await exec_with_timeout(
            asyncio.open_connection(host=self.host,
                                    port=self.port,
                                    ssl=self.ssl_context,
                                    loop=self.loop),
            self._connect_timeout,
            loop=self.loop
        )
        self._reader = reader
        self._writer = writer
        sock = writer.transport.get_extra_info('socket')
        if sock is not None:
            sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
            try:
                # TCP_KEEPALIVE
                if self.socket_keepalive:
                    sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
                    for k, v in self.socket_keepalive_options.items():
                        sock.setsockopt(socket.SOL_TCP, k, v)
            except (socket.error, TypeError):
                # `socket_keepalive_options` might contain invalid options
                # causing an error. Do not leave the connection open.
                writer.close()
                raise
        await self.on_connect() 
Example #18
Source File: vlc_device.py    From smart-tv-telegram with GNU Affero General Public License v3.0 5 votes vote down vote up
def _call(self, method: str, *args: typing.AnyStr):
        reader, writer = await asyncio.open_connection(self._params.host, self._params.port)
        headers = await reader.read(io.DEFAULT_BUFFER_SIZE)

        if headers.endswith(_AUTH_MAGIC):
            if self._params.password:
                writer.write(bytes(self._params.password, _ENCODING) + _EOF)
                await writer.drain()

            else:
                _LOGGER.error("vlc %s: need password", self._params.host)
                return writer.close()

        auth_result = await reader.read(io.DEFAULT_BUFFER_SIZE)

        if not auth_result.startswith(_AUTH_OK):
            _LOGGER.error("receive: %s", auth_result.decode(_ENCODING, "ignore"))
            return writer.close()

        writer.write(
            method.encode(_ENCODING) + b" " +
            b" ".join(a.encode(_ENCODING) for a in args) + _EOF
        )

        await writer.drain()
        return writer.close() 
Example #19
Source File: ptproxy.py    From ptproxy with MIT License 5 votes vote down vote up
def proxied_connection(dst, proxy_type=None, addr=None, port=None, rdns=True, username=None, password=None):
    if proxy_type == 'SOCKS4':
        socks4_addr = aiosocks.Socks4Addr(addr, port)
        socks4_auth = aiosocks.Socks4Auth(username)
        return aiosocks.open_connection(socks4_addr, socks4_auth, dst, remote_resolve=rdns)
    elif proxy_type == 'SOCKS5':
        socks5_addr = aiosocks.Socks5Addr(addr, port)
        socks5_auth = aiosocks.Socks5Auth(username, password)
        return aiosocks.open_connection(socks5_addr, socks5_auth, dst, remote_resolve=rdns)
    else:
        return asyncio.open_connection(*dst) 
Example #20
Source File: client.py    From naz with MIT License 5 votes vote down vote up
def connect(self, log_id: str = "") -> None:
        """
        make a network connection to SMSC server.
        """
        log_id = (
            log_id
            if log_id
            else "".join(random.choices(string.ascii_lowercase + string.digits, k=17))
        )
        try:
            self._log(
                logging.INFO, {"event": "naz.Client.connect", "stage": "start", "log_id": log_id}
            )
            reader, writer = await asyncio.wait_for(
                asyncio.open_connection(self.smsc_host, self.smsc_port), timeout=self.socket_timeout
            )
            self.reader = reader
            self.writer = writer
            self._log(
                logging.INFO, {"event": "naz.Client.connect", "stage": "end", "log_id": log_id}
            )
            self.current_session_state = SmppSessionState.OPEN
        except (
            OSError,
            ConnectionError,
            TimeoutError,
            # Note that `asyncio.TimeoutError` is raised with no msg/args.
            # So if logged as str(e) it would appear in logs as an empty string.
            # Instead we use repr(e) and it will appear as "TimeoutError()"
            # https://github.com/python/cpython/blob/723f71abf7ab0a7be394f9f7b2daa9ecdf6fb1eb/Lib/asyncio/tasks.py#L490
            asyncio.TimeoutError,
            socket.error,
            socket.herror,
            socket.gaierror,
            socket.timeout,
        ) as e:
            self._log(
                logging.ERROR,
                {"event": "naz.Client.connect", "stage": "end", "log_id": log_id, "error": repr(e)},
            ) 
Example #21
Source File: node_maintainer.py    From indy-node with Apache License 2.0 5 votes vote down vote up
def _open_connection_and_send(self, message: str):
        controlServiceHost = self.config.controlServiceHost
        controlServicePort = self.config.controlServicePort
        msgBytes = bytes(message, "utf-8")
        _, writer = await asyncio.open_connection(
            host=controlServiceHost,
            port=controlServicePort
        )
        writer.write(msgBytes)
        writer.close() 
Example #22
Source File: check_orphans.py    From paasta with Apache License 2.0 5 votes vote down vote up
def transfer_one_file(
    host: str, port: int = DEFAULT_NERVE_XINETD_PORT
) -> Tuple[str, Optional[str]]:
    logger.debug(f"getting file from {host}")
    try:
        reader, _ = await asyncio.wait_for(
            asyncio.open_connection(host=host, port=port, limit=2 ** 32), timeout=1.0
        )
        resp = await asyncio.wait_for(reader.read(), timeout=1.0)
    except (asyncio.TimeoutError, ConnectionRefusedError):
        logger.warning(f"error getting file from {host}")
        return (host, None)

    return (host, resp.decode()) 
Example #23
Source File: test_xmppserver.py    From bumper with GNU General Public License v3.0 5 votes vote down vote up
def test_xmpp_server():
    xmpp_address = ("127.0.0.1", 5223)
    xmpp_server = bumper.XMPPServer(xmpp_address)
    await xmpp_server.start_async_server()

    with LogCapture("xmppserver") as l:

        reader, writer = await asyncio.open_connection("127.0.0.1", 5223)

        writer.write(b"<stream:stream />")  # Start stream
        await writer.drain()

        await asyncio.sleep(0.1)

        assert len(xmpp_server.clients) == 1  # Client count increased
        assert (
            xmpp_server.clients[0].address[1]
            == writer.transport.get_extra_info("sockname")[1]
        )

        writer.close()  # Close connection
        await writer.wait_closed()

        await asyncio.sleep(0.1)

        assert len(xmpp_server.clients) == 0  # Client count decreased

        reader, writer = await asyncio.open_connection("127.0.0.1", 5223)

        writer.write(b"<stream:stream />")  # Start stream
        await writer.drain()

        await asyncio.sleep(0.1)

    xmpp_server.disconnect() 
Example #24
Source File: test_streams.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_drain_raises(self):
        # See http://bugs.python.org/issue25441

        # This test should not use asyncio for the mock server; the
        # whole point of the test is to test for a bug in drain()
        # where it never gives up the event loop but the socket is
        # closed on the  server side.

        q = queue.Queue()

        def server():
            # Runs in a separate thread.
            sock = socket.socket()
            sock.bind(('localhost', 0))
            sock.listen(1)
            addr = sock.getsockname()
            q.put(addr)
            clt, _ = sock.accept()
            clt.close()

        @asyncio.coroutine
        def client(host, port):
            reader, writer = yield from asyncio.open_connection(host, port, loop=self.loop)
            while True:
                writer.write(b"foo\n")
                yield from writer.drain()

        # Start the server thread and wait for it to be listening.
        thread = threading.Thread(target=server)
        thread.setDaemon(True)
        thread.start()
        addr = q.get()

        # Should not be stuck in an infinite loop.
        with self.assertRaises((ConnectionResetError, BrokenPipeError)):
            self.loop.run_until_complete(client(*addr))

        # Clean up the thread.  (Only on success; on failure, it may
        # be stuck in accept().)
        thread.join() 
Example #25
Source File: test_streams.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_open_connection_error(self):
        with test_utils.run_test_server() as httpd:
            conn_fut = asyncio.open_connection(*httpd.address,
                                               loop=self.loop)
            self._basetest_open_connection_error(conn_fut) 
Example #26
Source File: test_streams.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_open_connection_no_loop_ssl(self):
        with test_utils.run_test_server(use_ssl=True) as httpd:
            conn_fut = asyncio.open_connection(
                *httpd.address,
                ssl=test_utils.dummy_ssl_context(),
                loop=self.loop)

            self._basetest_open_connection_no_loop_ssl(conn_fut) 
Example #27
Source File: test_streams.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_open_connection(self):
        with test_utils.run_test_server() as httpd:
            conn_fut = asyncio.open_connection(*httpd.address,
                                               loop=self.loop)
            self._basetest_open_connection(conn_fut) 
Example #28
Source File: p2p_connection.py    From spruned with MIT License 5 votes vote down vote up
def connector_f(host=None, port=None, proxy=None):
    if proxy:
        return aiohttp_socks.open_connection(socks_url=proxy, host=host, port=port)
    return asyncio.open_connection(host=host, port=port) 
Example #29
Source File: tools.py    From spruned with MIT License 5 votes vote down vote up
def check_internet_connection():  # pragma: no cover
    global _last_internet_connection_check
    if _last_internet_connection_check and int(time.time()) - _last_internet_connection_check < 30:
        return True

    from spruned.application.context import ctx
    from spruned.application.logging_factory import Logger
    from spruned.settings import CHECK_NETWORK_HOST
    import asyncio
    if not ctx.proxy:
        Logger.root.debug('Checking internet connectivity')
        i = 0
        while i < 10:
            import random
            host = random.choice(CHECK_NETWORK_HOST)
            socket.setdefaulttimeout(3)
            try:
                socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, 53))
                _last_internet_connection_check = int(time.time())
                return True
            except:
                i += 1
                continue
        Logger.root.debug('No internet connectivity!')
    else:
        host, port = ctx.proxy.split(':')
        Logger.root.debug('Checking proxy connectivity')
        try:
            _last_internet_connection_check = int(time.time())
            p, t = await asyncio.open_connection(host=host, port=port)
            t.close()
            return True
        except Exception:
                Logger.root.debug('Cannot connect to proxy %s:%s', host, port, exc_info=True)
    return False 
Example #30
Source File: httptransport.py    From waspy with Apache License 2.0 5 votes vote down vote up
def connect(self, service, port, use_ssl):
        for _ in range(3):
            try:
                self.reader, self.writer = await \
                    asyncio.open_connection(service, port, ssl=use_ssl)
                return
            except ConnectionRefusedError:
                """ connection refused. Try again """
        raise ConnectionRefusedError(
            f'Connection refused to "{service}" on port {port}')