game_servers
Master Server Query Protocol
This module implements the legacy Steam master server protocol.
https://developer.valvesoftware.com/wiki/Master_Server_Query_Protocol
Nowadays games query servers through Steam. See steam.client.builtins.gameservers
Filters
Note
Multiple filters can be joined to together (Eg. \appid\730\white\1\empty\1)
Filter code |
What it does |
|---|---|
\nor\[x] |
A special filter, specifies that servers matching any of the following [x] conditions should not be returned |
\nand\[x] |
A special filter, specifies that servers matching all of the following [x] conditions should not be returned |
\dedicated\1 |
Servers running dedicated |
\secure\1 |
Servers using anti-cheat technology (VAC, but potentially others as well) |
\gamedir\[mod] |
Servers running the specified modification (ex. cstrike) |
\map\[map] |
Servers running the specified map (ex. cs_italy) |
\linux\1 |
Servers running on a Linux platform |
\password\0 |
Servers that are not password protected |
\empty\1 |
Servers that are not empty |
\full\1 |
Servers that are not full |
\proxy\1 |
Servers that are spectator proxies |
\appid\[appid] |
Servers that are running game [appid] |
\napp\[appid] |
Servers that are NOT running game [appid] (This was introduced to block Left 4 Dead games from the Steam Server Browser) |
\noplayers\1 |
Servers that are empty |
\white\1 |
Servers that are whitelisted |
\gametype\[tag,…] |
Servers with all of the given tag(s) in sv_tags |
\gamedata\[tag,…] |
Servers with all of the given tag(s) in their ‘hidden’ tags (L4D2) |
\gamedataor\[tag,…] |
Servers with any of the given tag(s) in their ‘hidden’ tags (L4D2) |
\name_match\[hostname] |
Servers with their hostname matching [hostname] (can use * as a wildcard) |
\version_match\[version] |
Servers running version [version] (can use * as a wildcard) |
\collapse_addr_hash\1 |
Return only one server for each unique IP address matched |
\gameaddr\[ip] |
Return only servers on the specified IP address (port supported and optional) |
Examples
Query HL Master
>>> for server_addr in gs.query_master(r'\appid\730\white\1', max_servers=3):
... print(server_addr)
...
('146.66.152.197', 27073)
('146.66.153.124', 27057)
('146.66.152.56', 27053)
Team Fortress 2 (Source)
>>> from steam import game_servers as gs
>>> server_addr = next(gs.query_master(r'\appid\40\empty\1\secure\1')) # single TF2 Server
>>> gs.a2s_ping(server_addr)
68.60899925231934
>>> gs.a2s_info(server_addr)
{'_ping': 74.61714744567871,
'_type': 'source',
'app_id': 40,
'bots': 0,
'environment': 'l',
'folder': u'dmc',
'game': u'DMC\t\t\t\t\t\t\t\t1',
'map': u'crossfire',
'max_players': 32,
'name': u'\t\t\u2605\t\t All Guns party \u2605\t \tCrossfire 24/7\t\t',
'players': 21,
'protocol': 48,
'server_type': 'd',
'vac': 1,
'visibility': 0}
>>> gs.a2s_players(server_addr)
[{'duration': 192.3097381591797, 'index': 0, 'name': '(2)Player', 'score': 4},
{'duration': 131.6618194580078, 'index': 1, 'name': 'BOLT', 'score': 2},
{'duration': 16.548809051513672, 'index': 2, 'name': 'Alpha', 'score': 0},
{'duration': 1083.1539306640625, 'index': 3, 'name': 'Player', 'score': 29},
{'duration': 446.7716064453125, 'index': 4, 'name': '(1)Player', 'score': 11},
{'duration': 790.9588012695312, 'index': 5, 'name': 'ИВАНГАЙ', 'score': 11}]
>>> gs.a2s_rules(server_addr)
{'amx_client_languages': 1,
'amx_nextmap': 'crossfire',
'amx_timeleft': '00:00',
'amxmodx_version': '1.8.2',
....
Ricohet (GoldSrc)
>>> from steam import game_servers as gs
>>> server_addr = next(gs.query_master(r'\appid\60')) # get a single ip from hl2 master
>>> gs.a2s_info(server_addr, force_goldsrc=True) # only accept goldsrc response
{'_ping': 26.59320831298828,
'_type': 'goldsrc',
'address': '127.0.0.1:27050',
'bots': 0,
'ddl': 0,
'download_link': '',
'environment': 'w',
'folder': 'ricochet',
'game': 'Ricochet',
'link': '',
'map': 'rc_deathmatch2',
'max_players': 32,
'mod': 1,
'name': 'Anitalink.com Ricochet',
'players': 1,
'protocol': 47,
'server_type': 'd',
'size': 0,
'type': 1,
'vac': 1,
'version': 1,
'visibility': 0}
API
- steam.game_servers.query_master(filter_text='\\nappid\\500', max_servers=20, region=MSRegion.World, master=('hl2master.steampowered.com', 27011), timeout=2)
Generator that returns (IP,port) pairs of servers
Warning
Valve’s master servers seem to be heavily rate limited. Queries that return a large numbers IPs will timeout before returning everything. There is no way to resume the query. Use
SteamClientto access game servers in a reliable way.Note
When specifying
filter_textuse raw strings otherwise python won’t treat backslashes as literal characters (e.g.query(r'\appid\730\white\1'))- Parameters:
- Raises:
- Returns:
a generator yielding (ip, port) pairs
- Return type:
generator
- steam.game_servers.a2s_info(server_addr, timeout=2, force_goldsrc=False, challenge=0)
Get information from a server
Note
All
GoldSrcgames have been updated to reply inSourceformat.GoldSrcformat is essentially DEPRECATED. By default the function will prefer to returnSourceformat, and will automatically fallback toGoldSrcif available.- Parameters:
- Raises:
- Returns:
a dict with information or None on timeout
- Return type:
- steam.game_servers.a2s_players(server_addr, timeout=2, challenge=0)
Get list of players and their info
- Parameters:
- Raises:
- Returns:
a list of players
- Return type:
- steam.game_servers.a2s_rules(server_addr, timeout=2, challenge=0, binary=False)
Get rules from server
- Parameters:
- Raises:
- Returns:
a list of rules
- Return type:
- steam.game_servers.a2s_ping(server_addr, timeout=2)
Ping a server
Warning
This method for pinging is considered deprecated and may not work on certian servers. Use
a2s_info()instead.- Parameters:
- Raises:
- Returns:
ping response in milliseconds or None for timeout
- Return type: