A library to query information of Goldsrc and Source servers.
Project description
SourceWatch
A Python 3 library for querying information from Valve's GoldSrc and Source game servers. This library provides a complete implementation of the Server Queries protocol used by games like Counter-Strike, Half-Life, Team Fortress, and many others.
Features
- Support for both GoldSrc and Source engine servers
- Query server information, player lists, and server rules
- Ping measurement functionality
- Multi-packet response handling
- Modern Python 3.8+ support with type hints (Pydantic)
- MCP ready
Installation
Install SourceWatch using pip:
pip install sourcewatch
Quick Start
import SourceWatch
# Connect to a server
server = SourceWatch.Query('server.example.com', 27015)
# Get basic server information
info = server.info()
print(f"Server: {info['info']['server_name']}")
print(f"Map: {info['info']['game_map']}")
print(f"Players: {info['info']['players_current']}/{info['info']['players_max_slots']}")
# Get player list
players = server.players()
for player in players['players']:
print(f"Player: {player['name']} (Score: {player['kills']})")
# Get server rules/settings
rules = server.rules()
for key, value in rules['rules'].items():
print(f"{key}: {value}")
# Measure ping
ping = server.ping()
print(f"Ping: {ping}ms")
API Reference
SourceWatch.Query(host, port=27015, timeout=10)
Main class for querying game servers.
Parameters:
host(str): Server hostname or IP addressport(int, optional): Server port (default: 27015)timeout(int, optional): Connection timeout in seconds (default: 10)
Methods:
info()
Returns basic server information including name, map, player count, and game details.
Returns: Dictionary with server information
{
"info": {
"server_name": "My Game Server",
"game_map": "de_dust2",
"game_title": "Counter-Strike: Source",
"players_current": 12,
"players_max_slots": 24,
"players_bots": 2,
"players_humans": 10,
"server_type": "d", # 'd' for dedicated, 'l' for listen
"server_os": "l", # 'l' for Linux, 'w' for Windows
"server_password_protected": 0,
"server_vac_secured": 1,
# ... additional fields
},
"server": {
"ip": "127.0.0.1",
"port": 27015,
"ping": 42.5
}
}
players()
Returns list of players currently on the server.
Returns: Dictionary with player information
{
"players": [
{
"index": 0,
"name": "PlayerName",
"kills": 15,
"play_time": 1234.5
},
# ... more players
],
"server": {
"ip": "127.0.0.1",
"port": 27015,
"ping": 45.2
}
}
rules()
Returns server configuration and rules.
Returns: Dictionary with server rules
{
"rules": {
"sv_gravity": "800",
"mp_friendlyfire": "1",
"mp_timelimit": "30",
# ... more rules
},
"server": {
"ip": "127.0.0.1",
"port": 27015,
"ping": 38.7
}
}
ping(num_requests=3)
Measures server response time by sending multiple info requests.
Parameters:
num_requests(int, optional): Number of ping requests to average (default: 3)
Returns: Average ping in milliseconds (float)
Supported Games
This library works with servers running games that use Valve's server query protocol, including:
-
Source Engine Games:
- Counter-Strike: Source
- Half-Life 2: Deathmatch
- Team Fortress 2
- Garry's Mod
- Left 4 Dead series
- Portal 2
-
GoldSrc Engine Games:
- Counter-Strike 1.6
- Half-Life
- Team Fortress Classic
- Day of Defeat
Error Handling
The library raises SourceWatchError for various error conditions:
import SourceWatch
try:
server = SourceWatch.Query('invalid-server.com')
info = server.info()
except SourceWatch.SourceWatchError as e:
print(f"Query failed: {e}")
except socket.timeout:
print("Connection timed out")
except socket.gaierror:
print("Could not resolve hostname")
Advanced Usage
Custom Timeout
# Set a custom timeout for slow connections
server = SourceWatch.Query('server.example.com', timeout=30)
Logging
Enable debug logging to see detailed protocol communication:
import logging
logging.basicConfig(level=logging.DEBUG)
server = SourceWatch.Query('server.example.com')
Development
Running Tests
python -m pytest test/
Requirements
- Python 3.8 or higher
- No external dependencies for core functionality
Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Credits
- Alex Kuhrt - Current maintainer
- Andreas Klauer - Original SourceLib implementation
This project is a modernized fork of SourceLib.
Changelog
Version 0.0.4
- Modernized packaging with pyproject.toml
- Fixed server query protocol implementation
- Added comprehensive test suite
- Improved error handling and logging
For more information, visit the GitHub repository or check out the issues page for bug reports and feature requests.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sourcewatch-0.0.9.tar.gz.
File metadata
- Download URL: sourcewatch-0.0.9.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
05dff936c0295bccc508c6a4fc4d55073c09130515b689a0a3a50dd1e03953d1
|
|
| MD5 |
d1285f56a55c6d4562fc0cf3bcf84307
|
|
| BLAKE2b-256 |
9000f001c508289368f201edc19d4aa0a227b43b57ad42f0386daf1ad0e9a340
|
File details
Details for the file sourcewatch-0.0.9-py3-none-any.whl.
File metadata
- Download URL: sourcewatch-0.0.9-py3-none-any.whl
- Upload date:
- Size: 11.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a033de3581f5eb178688fa64c5502542b9656651f36663f96e5f30bac41808d
|
|
| MD5 |
263e81aae0e8c3d811b5e48dc5ac161b
|
|
| BLAKE2b-256 |
2e83ec0ca64ddaf235ba18b744dc6b453da5899356a6a5c45c170c9987a0db6a
|