Skip to content

Instantly share code, notes, and snippets.

@koenbollen
Created October 25, 2010 22:44
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koenbollen/645947 to your computer and use it in GitHub Desktop.
Save koenbollen/645947 to your computer and use it in GitHub Desktop.
Query the Steam master servers.
#!/usr/bin/env python
#
# Query the Steam master servers.
#
# See: http://developer.valvesoftware.com/wiki/Master_Server_Query_Protocol
#
# Koen Bollen <meneer koenbollen nl>
# 2010 GPL
#
import socket
# Master server:
GOLD_SRC = ( "hl1master.steampowered.com", 27010 )
SOURCE = ( "hl2master.steampowered.com", 27011 )
# Regions:
US_EAST_COAST = 0x00
US_WEST_COAST = 0x01
SOUTH_AMERICA = 0x02
EUROPE = 0x03
ASIA = 0x04
AUSTRALIA = 0x05
MIDDLE_EAST = 0x06
AFRICA = 0x07
ALL = 0xFF
def _buildpacket( first, region, filters ):
first = "%s:%d" % first
filters = "".join( ["\\%s\\%s"%pair for pair in filters.items()] )
return "1%c%s\0%s\0" % ( region, first, filters )
def _parse( data ):
if data[:6] != "\xff\xff\xff\xfff\n":
return
data = data[6:]
while data:
ip = socket.inet_ntoa( data[:4] )
port = ord(data[4:5])<<8 | ord(data[5:6])
yield ip, port
data = data[6:]
def query( master=SOURCE, region=ALL, **filters ):
sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
sock.connect( master )
first = last = ( "0.0.0.0", 0 )
while True:
sock.sendall( _buildpacket( last, region, filters ) )
data = sock.recv(4096)
for server in _parse( data ):
last = server
if last != first:
yield server
if last == first:
break
if __name__ == "__main__":
print "Listing Team Fortress servers asynchronous..."
for server in query(gamedir='tf'):
print server
print "done"
print
print "Fetching Europe's Counter-Strike servers playing de_dust2..."
cstrike = set( query( region=EUROPE, gamedir="cstrike", map="de_dust2" ) )
print "done, length =", len( cstrike )
if len( set( query( gamedir="yourmom" ) ) ) > 0:
print "Wait... what?!"
# vim: expandtab
Copy link

ghost commented Jan 10, 2018

is it possible to create a masterserver using this method?

@tomkay1985
Copy link

Why the query stops everytime at a output of exactly 172.032 bytes?

@rx1983
Copy link

rx1983 commented Dec 31, 2023

not work, tem em php?

@koenbollen
Copy link
Author

This script was created "14 years ago".. 😅 I'm sure it doesn't work anymore and I can't even remember making it. Goodluck finding other solutions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment