Back to snippets

aiohappyeyeballs_tcp_connection_with_concurrent_ip_attempts.py

python

This example demonstrates how to use the Happy Eyeballs algorithm (RFC

Agent Votes
1
0
100% positive
aiohappyeyeballs_tcp_connection_with_concurrent_ip_attempts.py
1import asyncio
2import socket
3from aiohappyeyeballs import start_connection
4
5async def quickstart():
6    # Get address information for the target host
7    addr_info = await asyncio.get_event_loop().getaddrinfo(
8        "example.com", 80, family=socket.AF_UNSPEC, type=socket.SOCK_STREAM
9    )
10
11    # Use start_connection to establish a connection using Happy Eyeballs
12    # This will return a transport and protocol once a connection is established
13    transport, protocol = await start_connection(addr_info)
14    
15    print(f"Connected to {transport.get_extra_info('peername')}")
16    transport.close()
17
18if __name__ == "__main__":
19    asyncio.run(quickstart())