Protocol Classes#
This covers the Sans-I/O interface provided by little-a2s.
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.
- Parameters:
id (
int)- 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.
- Parameters:
data (
bytes)- 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.
Changed in version 0.2.0: Renamed to A2SGoldsourceClientProtocol from A2SClientGoldsourceProtocol.
- Parameters:
challenge (
int)
- 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.
- Parameters:
id (
int)- 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.
- Parameters:
data (
bytes)- 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.
- Parameters:
challenge (
int)
- 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.
- Parameters:
address (
str)name (
str)map (
str)folder (
str)game (
str)players (
int)max_players (
int)protocol (
int)type (
ServerType)environment (
Environment)visibility (
Visibility)mod (
GoldsourceMod|None)vac (
VAC)bots (
int)
- classmethod from_reader(reader)#
Parse this class from a reader.
- Parameters:
reader (
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.- Parameters:
- classmethod from_reader(reader)#
Parse this class from a reader.
- Parameters:
reader (
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:
ClientEvent,Sequence[Player]An A2S_PLAYER client protocol event.
Changed in version 0.5.1: Added support for the sequence protocol to interact with the underlying list of players.
- classmethod from_reader(reader)#
Parse this class from a reader.
- Parameters:
reader (
Reader)- Return type:
Self
- count(value) integer -- return number of occurrences of value#
- index(value[, start[, stop]]) integer -- return first index of value.#
Raises ValueError if the value is not present.
Supporting start and stop arguments is optional, but recommended.
- class little_a2s.ClientEventRules(*, rules)#
Bases:
ClientEvent,Mapping[bytes,bytes]An A2S_RULES client protocol event.
Changed in version 0.5.1: Added support for the mapping protocol to interact with the underlying dictionary of rules.
- classmethod from_reader(reader)#
Parse this class from a reader.
- Parameters:
reader (
Reader)- Return type:
Self
- get(k[, d]) D[k] if k in D, else d. d defaults to None.#
- items() a set-like object providing a view on D's items#
- keys() a set-like object providing a view on D's keys#
- values() an object providing a view on D's values#
-
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.
- Parameters:
- 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.
- Parameters:
link (
str)download_link (
str)version (
int)size (
int)type (
GoldsourceModType)dll (
GoldsourceModDLL)
- classmethod from_reader(reader)#
Parse this class from a reader.
- Parameters:
reader (
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:
- class little_a2s.Compression(*, size, checksum)#
Bases:
objectThe compression header for Source games. Mostly present in ~2006-era engines.
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.
- Parameters:
challenge (
int)
- class little_a2s.ClientPacketPlayers(*, challenge=-1)#
Bases:
ClientPacketAn A2S_PLAYER packet sent by the client.
- Parameters:
challenge (
int)
- class little_a2s.ClientPacketRules(*, challenge=-1)#
Bases:
ClientPacketAn A2S_RULES packet sent by the client.
- Parameters:
challenge (
int)
Utilities#
- class little_a2s.MultiPartResponse(*, id, total, compressed, payloads=<factory>)#
Bases:
objectA multi-part response waiting to be assembled.
Changed in version 0.2.0: Removed the
currentattribute.- add(header, payload)#
Add a payload with the given header to the response.
- Parameters:
header (
MultiHeader|MultiGoldsourceHeader)payload (
bytes)
- Return type:
- 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.
Added in version 0.2.0.
- little_a2s.first(t, it, /)#
Return the first element of the given type in an iterable.
Added in version 0.2.0.