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. Impliments Valve's Server Query Protocol. Rewrite of the python-valve module. Supports both synchronous and asyncronous applications.

Official demo application: Sourcequery

Requirements

Python >=3.7, 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

Examples

Example output shown may be shortened.

>>> import a2s
>>> address = ("stomping.kinofnemu.net", 27015)
>>> a2s.info(address)
SourceInfo(protocol=17, server_name=" 24/7 Dustbowl :: Nemu's Stomping Ground", map_name='cp_dustbowl',
folder='tf', game='Team Fortress', app_id=440, player_count=31, max_players=33, bot_count=21,
server_type='d', platform='l', password_protected=False, vac_enabled=True, version='5579073',
edf=177, port=27015, steam_id=85568392920040090, stv_port=None, stv_name=None,
keywords='brutus,celt,couch,cp,dustbowl,increased_maxplayers,nemu,nocrits,nodmgspread,pony,replays,vanilla',
game_id=440, ping=0.253798684978392)

>>> a2s.players(address)
[Player(index=0, name='Brutus', score=34, duration=836.4749145507812),
 Player(index=0, name='RageQuit', score=6, duration=1080.8099365234375),
 Player(index=0, name="Screamin' Eagles", score=1, duration=439.8598327636719)]

>>> a2s.rules(address)
{'coop': '0', 'deathmatch': '1', 'decalfrequency': '10', 'metamod_version': '1.10.7-devV',
 'mp_allowNPCs': '1', 'mp_autocrosshair': '1', 'mp_autoteambalance': '0', 'mp_disable_respawn_times': '0',
 'mp_fadetoblack': '0'}

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.

  • 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. 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.

Tested Games

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

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.3.0.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

python_a2s-1.3.0-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

File details

Details for the file python-a2s-1.3.0.tar.gz.

File metadata

  • Download URL: python-a2s-1.3.0.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.4.2 requests/2.25.0 setuptools/50.3.0 requests-toolbelt/0.8.0 tqdm/4.51.0 CPython/3.9.1

File hashes

Hashes for python-a2s-1.3.0.tar.gz
Algorithm Hash digest
SHA256 80bb86d7888b9706338144314bbff1d3faf87ff835892e7dcc9347ed27e37dc7
MD5 624e253e7797d7cfddc6d11f8329244a
BLAKE2b-256 7cd4e51624080990bf95e080ccb45c0087231936c29b44228663a796d4faff9e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: python_a2s-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 15.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.4.2 requests/2.25.0 setuptools/50.3.0 requests-toolbelt/0.8.0 tqdm/4.51.0 CPython/3.9.1

File hashes

Hashes for python_a2s-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ddcc148f1a2104c73b5478e513bef4643f91803a0db21954591c6152e0590df0
MD5 d787d2eea35de7a89e111b32026dd27f
BLAKE2b-256 15031bcfc5b25d52a6e0b92945af4137ff6dd8b86b5a26a25709a865d918d3b6

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