How to use the curio.socket.AF_INET function in curio

To help you get started, we’ve selected a few curio examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github guyingbo / shadowproxy / shadowproxy / __main__.py View on Github external
)
        host, port = parse_addr(qs["target"][0])
        kwargs["target_addr"] = (str(host), port)
    if "plugin" in qs:
        plugin_info = qs["plugin"][0]
        plugin_name, _, args = plugin_info.partition(";")
        args = [arg for arg in args.split(",") if arg]
        kwargs["plugin"] = plugins[plugin_name](*args)
    if "source_ip" in qs:
        kwargs["source_addr"] = parse_source_ip(qs, kwargs)
    if is_via:
        kwargs["uri"] = uri
        return ViaNamespace(ClientClass=proto, **kwargs)
    elif "via" in qs:
        kwargs["via"] = get_server(qs["via"][0], True)
    family = socket.AF_INET6 if ":" in bind_addr[0] else socket.AF_INET
    if url.scheme.endswith("udp"):
        server_sock = udp_server_socket(*bind_addr, family=family)
        real_ip, real_port, *_ = server_sock._socket.getsockname()
        server = run_udp_server(server_sock, proto(**kwargs))
    else:
        server_sock = curio.tcp_server_socket(*bind_addr, backlog=1024, family=family)
        real_ip, real_port, *_ = server_sock._socket.getsockname()
        server = run_server(
            server_sock, TcpProtoFactory(proto, **kwargs), ssl=get_ssl(url)
        )
    return server, (real_ip, real_port), url.scheme
github dabeaz / curio / examples / udp_echo_client.py View on Github external
async def main(addr):
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    await sock.connect(addr)
    for i in range(1000):
        msg = ('Message %d' % i).encode('ascii')
        print(msg)
        await sock.send(msg)
        resp = await sock.recv(1000)
        assert msg == resp
    await sock.close()
github guyingbo / shadowproxy / shadowproxy / __main__.py View on Github external
def udp_server_socket(host, port, *, family=socket.AF_INET, reuse_address=True):
    sock = socket.socket(family, socket.SOCK_DGRAM)
    try:
        if reuse_address:
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
        sock.bind((host, port))
        return sock
    except Exception:
        sock._socket.close()
        raise
github povilasb / hole-punching / holepunch / peer.py View on Github external
async def resolve_hostname(hostname: str, port: int=None) -> str:
    """DNS resolve hostname.

    Args:
        hostname: hostname to get IP address for.
        port: optional. Used to hint what DNS entry we're looking
            for.

    Returns:
        IP address used to connect to the specified hostname.
    """
    try:
        res = await socket.getaddrinfo(hostname, port, socket.AF_INET)
        if len(res) == 0:
            return None

        _, _, _, _, socket_addr = res[0]
        ip_addr, _ = socket_addr

        return ip_addr
    except socket.gaierror:
        return None
github guyingbo / shadowproxy / legency / shadowproxy_v0_2_5.py View on Github external
async def cmd_associate(self):
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        try:
            sock.bind(("", 0))
            host, port = sock.getsockname()
            async with sock:
                await self._stream.write(self._make_resp(host=host, port=port))
                task = await spawn(self.relay_udp(sock))
                while True:
                    data = await self._stream.read()
                    if not data:
                        await task.cancel()
                        return
                    if verbose > 0:
                        print("receive unexpect data:", data)
        except Exception:
            sock._socket.close()
github guyingbo / shadowproxy / legency / shadowproxy_v0_2_5.py View on Github external
async def udp_server(
    host, port, handler_task, *, family=socket.AF_INET, reuse_address=True
):
    sock = socket.socket(family, socket.SOCK_DGRAM)
    try:
        sock.bind((host, port))
        if reuse_address:
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
        async with sock:
            await handler_task(sock)
    except Exception:
        sock._socket.close()
        raise
github dabeaz / curio / curio / network.py View on Github external
def tcp_server_socket(host, port, family=socket.AF_INET, backlog=100,
                      reuse_address=True, reuse_port=False):

    sock = socket.socket(family, socket.SOCK_STREAM)
    try:
        if reuse_address:
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)

        if reuse_port:
            try:
                sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, True)
            except (AttributeError, OSError) as e:
                log.warning('reuse_port=True option failed', exc_info=True)

        sock.bind((host, port))
        sock.listen(backlog)
    except Exception:
github guyskk / curethinkdb / curethinkdb / net.py View on Github external
async def connect(self):
        # ssl_context = ssl.create_default_context()
        # ssl_context.verify_mode = ssl.CERT_REQUIRED
        # ssl_context.check_hostname = True  # redundant with match_hostname
        # self._socket = await open_connection(self.host, self.port, ssl=ssl)
        self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        await self._socket.connect((self.host, self.port))
        self._socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
        try:
            self._handshake.reset()
            response = None
            while True:
                request = self._handshake.next_message(response)
                if request is None:
                    break
                # This may happen in the `V1_0` protocol where we send two requests as
                # an optimization, then need to read each separately
                if request:
                    await self.sendall(request)
                # The response from the server is a null-terminated string
                response = (await self.read_until(b'\0'))[:-1]