Asynchronous Clients#
Added in version 0.5.0.
This covers the asynchronous I/O client provided by little-a2s.
little-a2s only provides an asyncio client. If you need to integrate with a different event loop such as Trio, anyio, or gevent, you can use the Sans-I/O interface to build your own client.
- class little_a2s.AsyncA2S(connector)#
Bases:
DatagramProtocolAn asynchronous client for A2S queries.
a2s = AsyncA2S.from_addr("127.0.0.1", 27015) async with a2s, asyncio.timeout(1): print(await a2s.info()) print(await a2s.players()) print(await a2s.rules())
This follows the Source format. For the Goldsource equivalent, see
AsyncA2SGoldsource.This class supports the asynchronous context manager protocol which calls the connector function to set the transport + remote address, and closes the transport upon exit.
- Parameters:
connector (
Callable[[Self],Awaitable[tuple[str,int] |tuple[str,int,int,int] |tuple[int,bytes] |None]]) – The function to call and await to create a datagram transport and return the remote address, if any. See alsofrom_addr(),from_ipv4(), andfrom_ipv6().
Added in version 0.5.0.
- classmethod from_addr(host, port, *, prefer_ipv4=True)#
Resolve the given host and create an A2S query.
- classmethod from_ipv4()#
Create an A2S query with a UDP IPv4 socket not connected to any address.
This allows you to use the same socket with
addrarguments:async with AsyncA2S.from_ipv4() as a2s, asyncio.timeout(1): info = await a2s.info(("127.0.0.1", 2303)) info = await a2s.info(("127.0.0.1", 27015))
- Return type:
Self
- classmethod from_ipv6()#
Create an A2S query with a UDP IPv6 socket not connected to any address.
This allows you to use the same socket with
addrarguments:async with AsyncA2S.from_ipv6() as a2s, asyncio.timeout(1): info = await a2s.info(("::1", 2303)) info = await a2s.info(("::1", 27015))
- Return type:
Self
- async close()#
Close the current datagram transport, raising any exception if the connection improperly closed.
- Raises:
RuntimeError – The transport is not connected.
- Return type:
- async info(addr=None)#
Send an A2S_INFO request and wait for a response.
- Parameters:
addr (
tuple[str,int] |tuple[str,int,int,int] |tuple[int,bytes] |None) – The address to send the request to. Does not apply if socket is already connected to an address, such as fromfrom_addr().- Raises:
ChallengeError – The server sent too many challenges.
PayloadError – The server sent a malformed packet.
TimeoutError – The server did not respond.
TypeError – The addr argument was required or forbidden.
- Return type:
- async players(addr=None)#
Send an A2S_PLAYER request and wait for a response.
- Parameters:
addr (
tuple[str,int] |tuple[str,int,int,int] |tuple[int,bytes] |None) – The address to send the request to. Does not apply if socket is already connected to an address, such as fromfrom_addr().- Raises:
ChallengeError – The server sent too many challenges.
PayloadError – The server sent a malformed packet.
TimeoutError – The server did not respond.
TypeError – The addr argument was required or forbidden.
- Return type:
- async rules(addr=None)#
Send an A2S_RULES request and wait for a response.
- Parameters:
addr (
tuple[str,int] |tuple[str,int,int,int] |tuple[int,bytes] |None) – The address to send the request to. Does not apply if socket is already connected to an address, such as fromfrom_addr().- Raises:
ChallengeError – The server sent too many challenges.
PayloadError – The server sent a malformed packet.
TimeoutError – The server did not respond.
TypeError – The addr argument was required or forbidden.
- Return type:
- async start()#
Call the connector function to create the datagram transport.
- Raises:
OSError – The address could not be resolved.
RuntimeError – The transport is already connected.
- Return type:
- property transport: DatagramTransport#
The current datagram transport.
- Raises:
RuntimeError – The transport is not connected.
- class little_a2s.AsyncA2SGoldsource(connector)#
Bases:
AsyncA2SA asynchronous client for A2S Goldsource queries.
- Parameters:
connector (
Callable[[Self],Awaitable[tuple[str,int] |tuple[str,int,int,int] |tuple[int,bytes] |None]])
- async info(addr=None)#
Send an A2S_INFO request and wait for a response.
- Parameters:
addr (
tuple[str,int] |tuple[str,int,int,int] |tuple[int,bytes] |None) – The address to send the request to. Does not apply if socket is already connected to an address, such as fromfrom_addr().- Raises:
ChallengeError – The server sent too many challenges.
PayloadError – The server sent a malformed packet.
TimeoutError – The server did not respond.
TypeError – The addr argument was required or forbidden.
- Return type: