Synchronous Clients#
This covers the synchronous I/O client provided by little-a2s.
- class little_a2s.A2S(sock, *, challenge=-1)#
Bases:
objectA synchronous client for A2S queries.
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.settimeout(1) sock.connect(("127.0.0.1", 27015)) with A2S(sock) as a2s: print(a2s.info()) print(a2s.players()) print(a2s.rules())
This follows the Source format. For the Goldsource equivalent, see
A2SGoldsource.This class supports the context manager protocol which automatically closes the socket upon exit.
- Parameters:
sock (
socket) – The UDP socket to send and receive queries from. The socket must be connected to a remote address beforehand withconnect(). You may also want to set a timeout withsettimeout(). Alternatively, usefrom_addr()to construct the socket for you.challenge (
int) – The initial challenge sequence to use for requests. This is optional if you close the socket and want to resume sending queries shortly afterwards without an extra challenge response. However, the server may still challenge you regardless.
- classmethod from_addr(host, port, timeout=3.0, *, prefer_ipv4=True)#
Resolve the given host and create an A2S query.
- Parameters:
host (
str) – The IPv4 address, IPv6 address, or domain name to query.port (
int) – The port to query.prefer_ipv4 (
bool) – If True, prefer to resolve hostnames to IPv4 addresses. This may still connect the socket to an IPv6 address so if you need more control, consider usingsocket.getaddrinfo()to manually create a socket and pass it to the constructor.
- Raises:
OSError – The address could not be resolved.
- Return type:
Self
- events_received()#
Purge all outstanding events not returned by other methods.
- Return type:
- info()#
Send an A2S_INFO request and wait for a response.
- Raises:
TimeoutError – The socket timed out.
ValueError – The server sent a malformed packet.
- Return type:
- players()#
Send an A2S_PLAYER request and wait for a response.
- Raises:
TimeoutError – The socket timed out.
ValueError – The server sent a malformed packet.
- Return type:
- rules()#
Send an A2S_RULES request and wait for a response.
- Raises:
TimeoutError – The socket timed out.
ValueError – The server sent a malformed packet.
- Return type:
- class little_a2s.A2SGoldsource(sock, *, challenge=-1)#
Bases:
A2SA synchronous client for A2S Goldsource queries.
- classmethod from_addr(host, port, timeout=3.0, *, prefer_ipv4=True)#
Resolve the given host and create an A2S query.
- Parameters:
host (
str) – The IPv4 address, IPv6 address, or domain name to query.port (
int) – The port to query.prefer_ipv4 (
bool) – If True, prefer to resolve hostnames to IPv4 addresses. This may still connect the socket to an IPv6 address so if you need more control, consider usingsocket.getaddrinfo()to manually create a socket and pass it to the constructor.
- Raises:
OSError – The address could not be resolved.
- Return type:
Self
- events_received()#
Purge all outstanding events not returned by other methods.
- Return type:
- info()#
Send an A2S_INFO request and wait for a response.
- Raises:
TimeoutError – The socket timed out.
ValueError – The server sent a malformed packet.
- Return type:
- players()#
Send an A2S_PLAYER request and wait for a response.
- Raises:
TimeoutError – The socket timed out.
ValueError – The server sent a malformed packet.
- Return type:
- rules()#
Send an A2S_RULES request and wait for a response.
- Raises:
TimeoutError – The socket timed out.
ValueError – The server sent a malformed packet.
- Return type: