little-a2s documentation#
A synchronous and sans-I/O library implementing the A2S Valve Source Query protocol.
from little_a2s import A2S
with A2S.from_addr("example.com", 27015, timeout=1) as a2s:
print(a2s.info())
print(a2s.players())
print(a2s.rules())
Installation#
This project requires Python 3.11 or newer. Install it from PyPI by using pip:
$ python3 -m venv
$ .venv/bin/activate
(.venv) $ pip install little-a2s
Synchronous Clients#
- class little_a2s.A2S(sock, *, challenge=-1)#
Bases:
objectA synchronous client for A2S queries.
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(). See alsofrom_addr().challenge (
int) – The initial challenge sequence to use for requests. This is only needed if you close the socket and need to resume sending queries again.
- classmethod from_addr(host, port, timeout=3.0)#
Resolve the given host and create an A2S query.
- 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)#
Resolve the given host and create an A2S query.
- 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:
Protocols#
- class little_a2s.A2SClientProtocol(*, challenge=-1)#
Bases:
objectImplements the client-side protocol for A2S.
This follows the Source format. For the Goldsource equivalent, see
A2SGoldsourceClientProtocol.- Parameters:
challenge (
int) – The challenge sequence to use for requests. This can change dynamically when receiving S2C_CHALLENGE responses.
- events_received()#
Return a list of events received since this last call.
- Return type:
- info()#
Create an A2S_INFO packet to send to the server.
- Return type:
- invalidate_response(id)#
Invalidate an incomplete multi-part response with the given ID.
This should be invoked after some sort of timeout.
- Return type:
- packets_to_send()#
Return a list of packets to send since this last call.
- Return type:
- players()#
Create an A2S_PLAYER packet to send to the server.
- Return type:
- receive_datagram(data)#
Process a packet from the server.
- Raises:
ValueError – The data is malformed.
- Return type:
- rules()#
Create an A2S_RULES packet to send to the server.
- Return type:
- class little_a2s.A2SGoldsourceClientProtocol(*, challenge=-1)#
Bases:
A2SClientProtocolThe Goldsource variant of the A2S client protocol used by older games.
- events_received()#
Return a list of events received since this last call.
- Return type:
- info()#
Create an A2S_INFO packet to send to the server.
- Return type:
- invalidate_response(id)#
Invalidate an incomplete multi-part response with the given ID.
This should be invoked after some sort of timeout.
- Return type:
- packets_to_send()#
Return a list of packets to send since this last call.
- Return type:
- players()#
Create an A2S_PLAYER packet to send to the server.
- Return type:
- receive_datagram(data)#
Process a packet from the server.
- Raises:
ValueError – The data is malformed.
- Return type:
- rules()#
Create an A2S_RULES packet to send to the server.
- Return type:
Events#
- class little_a2s.ClientEventChallenge(*, challenge)#
Bases:
ClientEventAn S2C_CHALLENGE client protocol event.
- classmethod from_reader(reader)#
Parse this class from a reader.
- Return type:
Self
- class little_a2s.ClientEventGoldsourceInfo(*, address, name, map, folder, game, players, max_players, protocol, type, environment, visibility, mod, vac, bots)#
Bases:
ClientEventAn A2S_INFO Goldsource client protocol event.
- classmethod from_reader(reader)#
Parse this class from a reader.
- Return type:
Self
-
environment:
Environment# Indicates the operating system of the server.
-
mod:
GoldsourceMod|None# Information about the Goldsource mod, if the game is a mod.
-
type:
ServerType# Indicates the type of server.
-
visibility:
Visibility# Indicates whether the server requires a password.
- class little_a2s.ClientEventInfo(*, protocol, name, map, folder, game, id, players, max_players, bots, type, environment, visibility, vac, version, extra)#
Bases:
ClientEventAn A2S_INFO client protocol event.
This follows the Source format. For the Goldsource equivalent, see
ClientEventGoldsourceInfo.- classmethod from_reader(reader)#
Parse this class from a reader.
- Return type:
Self
-
environment:
Environment# Indicates the operating system of the server.
-
type:
ServerType# Indicates the type of server.
-
visibility:
Visibility# Indicates whether the server requires a password.
- class little_a2s.ClientEventPlayers(*, players)#
Bases:
ClientEventAn A2S_PLAYER client protocol event.
- classmethod from_reader(reader)#
Parse this class from a reader.
- Return type:
Self
- class little_a2s.ClientEventRules(*, rules)#
Bases:
ClientEventAn A2S_RULES client protocol event.
- classmethod from_reader(reader)#
Parse this class from a reader.
- Return type:
Self
-
rules:
dict[bytes,bytes]# The server rules or configuration variables.
While the protocol states these should be UTF-8 strings, some games might provide binary data in rules which may not decode correctly as UTF-8. If you know the game doesn’t do this, you can use the
decode()method for convenience.
- class little_a2s.Environment(*values)#
Bases:
_EnumReprMixin,IntEnumIndicates the operating system of the server.
- class little_a2s.ExtraInfo(*, port=None, steam_id=None, spectator_port=None, spectator_name=None, keywords=None, game_id=None)#
Bases:
objectExtra data included with an A2S_INFO response.
- classmethod from_reader(reader, flag)#
Parse this class from a reader.
- class little_a2s.GoldsourceMod(*, link, download_link, version, size, type, dll)#
Bases:
objectInformation about the Goldsource mod, if the game is a mod.
- classmethod from_reader(reader)#
Parse this class from a reader.
- Return type:
Self
-
dll:
GoldsourceModDLL# Indicates whether the mod uses its own DLL.
-
type:
GoldsourceModType# Indicates the type of mod.
- class little_a2s.GoldsourceModDLL(*values)#
Bases:
_EnumReprMixin,IntEnumIndicates whether a
GoldsourceModuses its own DLL.- EXTENSION = 1#
This mod provides its own DLL.
- NATIVE = 0#
This mod uses the Half-Life DLL.
- class little_a2s.GoldsourceModType(*values)#
Bases:
_EnumReprMixin,IntEnumIndicates the type of mod for
GoldsourceMod.
- class little_a2s.Player(*, index, name, score, duration)#
Bases:
objectA player returned in the A2S_PLAYER response.
Headers#
- class little_a2s.HeaderType(*values)#
Bases:
_EnumReprMixin,IntEnum- MULTI = -2#
Means the response is split.
- SIMPLE = -1#
Means the response isn’t split.
- class little_a2s.MultiHeader(*, id, current, total, size, compressed)#
Bases:
HeaderA multi-packet A2S response header for Source games.
-
compressed:
Compression|None# An optional compression header. Mostly present in ~2006-era engines.
-
compressed:
Packets#
- class little_a2s.ClientPacket(*, header, payload, challenge)#
Bases:
PacketAn A2S packet sent by the client.
This class and its subclasses can be coerced into byte streams by passing to
bytes(), for examplebytes(packet).
- class little_a2s.ClientPacketInfo(*, challenge=-1)#
Bases:
ClientPacketAn A2S_INFO packet sent by the client.
- class little_a2s.ClientPacketPlayers(*, challenge=-1)#
Bases:
ClientPacketAn A2S_PLAYER packet sent by the client.
Utilities#
- class little_a2s.MultiPartResponse(*, id, total, compressed, payloads=<factory>)#
Bases:
objectA multi-part response waiting to be assembled.
- assemble()#
Assemble the payload together if all parts have been received.
-
compressed:
Compression|None# An optional compression header. Mostly present in ~2006-era engines.
- class little_a2s.Reader(data)#
Bases:
objectA simple reader for parsing serialized data.
- read(n=-1, /)#
Read exactly n bytes, or the entire stream if n is negative.
- read_byte()#
Read a single unsigned byte.
- read_char()#
Read a single ASCII character.
- read_float()#
Read a 32-bit float.
- read_long()#
Read a signed 32-bit integer.
- read_null()#
Read a null byte.
- Raises:
EOFError – Not enough bytes could be read.
ValueError – A non-null byte was read.
- Return type:
Literal[0]
- read_null_string()#
Read a null-terminated byte string.
- read_null_utf8()#
Read a null-terminated, UTF-8 decoded string.
- Raises:
EOFError – Not enough bytes could be read.
UnicodeDecodeError – The string could not be decoded as UTF-8.
- Return type:
- read_short()#
Read a signed 16-bit integer.
- read_uint64()#
Read an unsigned 64-bit integer.
- read_ulong()#
Read an unsigned 32-bit integer.
- read_until(sep)#
Read until the sep character is found and return all bytes before sep.
- read_ushort()#
Read an unsigned 16-bit integer.
- read_varchar1()#
Read a byte
lengthand then return exactlylengthbytes.
- read_varchar2()#
Read a 2-byte
lengthand then return exactlylengthbytes.
- read_varchar4()#
Read a 4-byte
lengthand then return exactlylengthbytes.
- little_a2s.filter_type(t, it, /)#
Filter through an iterable for elements of the given type.
- little_a2s.first(t, it, /)#
Return the first element of the given type in an iterable.