Skip to main content

Query Source and GoldSource servers for name, map, players and more.

Project description

Python A2S

Library to query Source and GoldSource servers. Implements Valve's Server Query Protocol. Rewrite of the python-valve module. Supports both synchronous and asyncronous applications.

Official demo application: Sourcequery

Requirements

Python >=3.9, no external dependencies

Install

pip3 install python-a2s or python3 setup.py install

API

Functions

  • a2s.info(address, timeout=DEFAULT_TIMEOUT, encoding=DEFAULT_ENCODING)
  • a2s.players(address, timeout=DEFAULT_TIMEOUT, encoding=DEFAULT_ENCODING)
  • a2s.rules(address, timeout=DEFAULT_TIMEOUT, encoding=DEFAULT_ENCODING)

All functions also have an async version as of package 1.2.0 that adds an a prefix, e.g. ainfo, aplayers, arules.

Parameters

  • address: Tuple[str, int] - Address of the server.
  • timeout: float - Timeout in seconds. Default: 3.0
  • encoding: str or None - String encoding, None disables string decoding. Default: utf-8

Return Values

  • info: SourceInfo or GoldSrcInfo. They are documented in the source file.
  • players: List of Player items. Also documented in the corresponding source file.
  • rules: Dictionary of key - value pairs.

Exceptions

  • a2s.BrokenMessageError(Exception) - General decoding error
  • a2s.BufferExhaustedError(BrokenMessageError) - Response too short
  • socket.timeout - No response (synchronous calls)
  • asyncio.exceptions.TimeoutError - No response (async calls)
  • socket.gaierror - Address resolution error
  • ConnectionRefusedError - Target port closed
  • OSError - Various networking errors like routing failure

Examples

Example output shown may be shortened. Also the server shown in the example may be down by the time you see this.

>>> import a2s
>>> address = ("chi-1.us.uncletopia.com", 27015)
>>> a2s.info(address)
SourceInfo(protocol=17, server_name='Uncletopia | Chicago | 1', map_name='pl_badwater',
folder='tf', game='Team Fortress', app_id=440, player_count=24, max_players=24, bot_count=0,
server_type='d', platform='l', password_protected=False, vac_enabled=True, version='7370160',
edf=241, port=27015, steam_id=85568392924469984, stv_port=27016,
stv_name='Uncletopia | Chicago | 1 | STV', keywords='nocrits,nodmgspread,payload,uncletopia',
game_id=440, ping=0.2339219159912318)

>>> a2s.players(address)
[Player(index=0, name='AmNot', score=22, duration=8371.4072265625),
Player(index=0, name='TAAAAANK!', score=15, duration=6251.03173828125),
Player(index=0, name='Tiny Baby Man', score=17, duration=6229.0361328125)]

>>> a2s.rules(address)
{'coop': '0', 'cronjobs_version': '2.0', 'crontab_version': '2.0', 'deathmatch': '1',
'decalfrequency': '10', 'discord_accelerator_version': '1.0', 'discord_version': '1.0',
'extendedmapconfig_version': '1.1.1', 'metamod_version': '1.11.0-dev+1145V', 'mp_allowNPCs': '1'}

Notes

  • Some servers return inconsistent or garbage data. Filtering this out is left to the specific application, because there is no general approach to filtering that makes sense for all use cases. In most scenarios, it makes sense to at least remove players with empty names. Also the player_count value in the info query and the actual number of players returned in the player query do not always match up. Sometimes the player query returns an empty list of players.

  • For some games, the query port is different from the actual connection port. The Steam server browser will show the connection port and querying that will not return an answer. There does not seem to be a general solution to this problem so far, but usually probing port numbers up to 10 higher and lower than the connection port usually leads to a response. There's also the option of using http://api.steampowered.com/ISteamApps/GetServersAtAddress/v0001?addr={IP} to get a list of game servers on an IP (thanks to Nereg for this suggestion). If you're still not successful, use a network sniffer like Wireshark to monitor outgoing packets while refreshing the server popup in Steam.

  • Player counts above 255 do not work and there's no way to make them work. This is a limitation in the specification of the protocol.

  • This library does not implement rate limiting. It's up to the application to limit the number of requests per second to an acceptable amount to not trigger any firewall rules.

Tested Games

Half-Life 2, Half-Life, Team Fortress 2, Counter-Strike: Global Offensive, Counter-Strike 1.6, ARK: Survival Evolved, Rust

Similar Projects

  • dayzquery - Module for decoding DayZ rules responses
  • l4d2query - Module for querying additional data from L4D2 servers

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_a2s-1.4.0.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

python_a2s-1.4.0-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file python_a2s-1.4.0.tar.gz.

File metadata

  • Download URL: python_a2s-1.4.0.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for python_a2s-1.4.0.tar.gz
Algorithm Hash digest
SHA256 72e46bf9f0a82eda83b9ed7e61fce36c1728cd7e8a2bcdadedb33b2cb05a0141
MD5 45ea34d3f12d572b27eb90f27a71ded8
BLAKE2b-256 35bd2a2bade2a15f219e4849f0da3c4de02e91f6424aca65591e99f90325331d

See more details on using hashes here.

File details

Details for the file python_a2s-1.4.0-py3-none-any.whl.

File metadata

  • Download URL: python_a2s-1.4.0-py3-none-any.whl
  • Upload date:
  • Size: 13.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for python_a2s-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 215b181a707507c242c0284b2702441bf7ee6a5f803757297bf6243af37bb73c
MD5 652f073c1b57295651969f6effd8373c
BLAKE2b-256 cb92eed28acdf45e20d73bb9ce179b19c3722703bf5037c922f3a6f39b2b5641

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page