Skip to content

SourceServer

Derpius edited this page Sep 18, 2021 · 6 revisions

SourceServer Object

Overview

The SourceServer object represents an instance of a source dedicated server, and provides methods for querying the server it represents using UDP.
To create a new instance of a server, simply pass a connection string (ipv4:port) to the constructor.

Exception Handling

Any errors that the server anticipates (like a bad response packet, or connection timeout) are raised as a SourceError, which closes the connection to the server.
Note that the actual socket connection to the server is never closed, but the instance is simply marked as closed until a successful retry (see methods table).

Fields

Name Description
info Property that performs an info request (see below)
rules Property that performs a rules request (see below)
constr Connection string (as passed to the constructor)
isClosed Whether or not the server connection is closed

Methods

Method Description
close() Marks the server as closed
retry() Attempts to reconnect to the server
ping(places: int = 0) Returns the ping to the server rounded to places decimal places
getPlayers() Returns a tuple of players. See below for the structure of each player

Each player is a tuple in the form (index: int, name: str, score: int, duration: float), unless the server is running The Ship, in which case each player tuple is in the form (index: int, name: str, score: int, duration: float, deaths: int, money: int)

.info Property

The .info property may look like a variable, but is, in fact, a property that sends an info query to the server and returns the response when read.
Any attempts to set this will raise an AttributeError.

.info Property Values

Note, this table is a modified version of the table found here
Also, you don't need to worry about the technical info on certain values (EDF and possible truncated id for example) as this is handled automatically, just use it for reference regarding the contents of .info. If in doubt, print its contents after connecting to a server

Field Name Type Description
protocol byte Protocol version used by the server.
name str Name of the server.
map str Map the server has currently loaded.
folder str Name of the folder containing the game files.
game str Full name of the game.
id int Steam Application ID of game.
players byte Number of players on the server.
max_players byte Maximum number of players the server reports it can hold.
bots byte Number of bots on the server.
server_type byte Indicates the type of server:
  • 'd' for a dedicated server
  • 'l' for a non-dedicated server
  • 'p' for a SourceTV relay (proxy)
environment byte Indicates the operating system of the server:
  • 'l' for Linux
  • 'w' for Windows
  • 'm' or 'o' for Mac (the code changed after L4D1)
visibility byte Indicates whether the server requires a password:
  • 0 for public
  • 1 for private
VAC byte Specifies whether the server uses VAC:
  • 0 for unsecured
  • 1 for secured
These fields only exist in a response if the server is running The Ship:
Data Type Comment
mode byte Indicates the game mode:
  • 0 for Hunt
  • 1 for Elimination
  • 2 for Duel
  • 3 for Deathmatch
  • 4 for VIP Team
  • 5 for Team Elimination
witnesses byte The number of witnesses necessary to have a player arrested.
duration byte Time (in seconds) before a player is arrested while being witnessed.
version str Version of the game installed on the server.
EDF byte If present, this specifies which additional data fields will be included.
Only if EDF & 0x80 proves true:
Data Type Comment
port int The server's game port number.
Only if EDF & 0x10 proves true:
Data Type Comment
steam_id int Server's SteamID.
Only if EDF & 0x40 proves true:
Data Type Comment
sourceTV_port int Spectator port number for SourceTV.
sourceTV_name str Name of the spectator server for SourceTV.
Only if EDF & 0x20 proves true:
Data Type Comment
keywords str Tags that describe the game according to the server (for future use.)
Only if EDF & 0x01 proves true:
Data Type Comment
game_id int The server's 64-bit GameID. If this is present, a more accurate AppID is present in the low 24 bits.
The earlier AppID could have been truncated as it was forced into 16-bit storage.

.rules Property

Like the .info property, this performs a rules request when its getter is called, returning a dictionary of the key-value pair server rules (convars).