Synchronous Clients#

This covers the synchronous I/O client provided by little-a2s.

class little_a2s.A2S(sock, *, challenge=-1)#

Bases: object

A 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 with connect(). You may also want to set a timeout with settimeout(). Alternatively, use from_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.

  • timeout (float | None) – The timeout to set on the socket.

  • 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 using socket.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:

list[ClientEvent]

info()#

Send an A2S_INFO request and wait for a response.

Raises:
Return type:

ClientEventInfo

players()#

Send an A2S_PLAYER request and wait for a response.

Raises:
Return type:

ClientEventPlayers

rules()#

Send an A2S_RULES request and wait for a response.

Raises:
Return type:

ClientEventRules

class little_a2s.A2SGoldsource(sock, *, challenge=-1)#

Bases: A2S

A synchronous client for A2S Goldsource queries.

Parameters:
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.

  • timeout (float | None) – The timeout to set on the socket.

  • 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 using socket.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:

list[ClientEvent]

info()#

Send an A2S_INFO request and wait for a response.

Raises:
Return type:

ClientEventGoldsourceInfo

players()#

Send an A2S_PLAYER request and wait for a response.

Raises:
Return type:

ClientEventPlayers

rules()#

Send an A2S_RULES request and wait for a response.

Raises:
Return type:

ClientEventRules