如何利用Steam API获取游戏服务器的玩家数量?
Hey there! Let's walk through exactly how to pull the current player count from a Steam game server using the Steam API. I'll cover two reliable methods depending on your use case.
Method 1: Steam Web API (HTTP Request)
This is the easiest approach for most scenarios, since it uses standard HTTP calls and returns clean JSON data.
Prerequisites
- A Steam Web API Key: You can grab one from the Steam Developer Portal (just log in with your Steam account, navigate to the API section, and generate a key—keep this secret, don't share it publicly!).
- The target server's IP address and game port (not the query port, unless specified otherwise).
- Optional: The game's AppID (some endpoints require this to validate access).
The Request
Use the ISteamGameserver/GetServerPlayers/v1/ endpoint. Here's the format for the request URL:
https://api.steampowered.com/ISteamGameserver/GetServerPlayers/v1/?key=YOUR_API_KEY&addr=SERVER_IP&port=SERVER_PORT
Parse the Response
The API returns a JSON object where you can directly grab the player count. Here's a sample response snippet:
{ "response": { "player_count": 7, "max_players": 16, "players": [ {"name": "JohnDoe", "score": 120, "time_played": 4567}, // ... other player objects ] } }
Just access response.player_count to get the current number of players on the server.
Notes
- If you get an error, double-check your API key permissions and make sure the server allows Web API queries.
- For bulk server queries, pair this with
ISteamMasterServerQuery/GetServerListto fetch a list of servers first, then loop through them to get player counts.
Method 2: Direct UDP Server Query
If the Web API isn't available (some servers disable it), you can use Steam's native UDP query protocol to communicate directly with the server. This is more low-level but works for almost all Steam games.
How It Works
Steam servers listen on a query port (usually the game port + 1, e.g., game port 27015 → query port 27016). You send a specific UDP packet and parse the response to extract player counts.
Example Python Code
Here's a simplified script to demonstrate:
import socket def fetch_player_count(server_ip, query_port): # Set up UDP socket sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.settimeout(5) # Send A2S_INFO query (standard Steam server info request) query_packet = b'\xFF\xFF\xFF\xFFTSource Engine Query\x00' sock.sendto(query_packet, (server_ip, query_port)) try: response = sock.recv(4096) # Parse the response (offsets vary slightly by game, but this works for most Source games) current_players = response[12] max_players = response[13] return f"Current players: {current_players}/{max_players}" except socket.timeout: return "Error: Server did not respond" finally: sock.close() # Test it out print(fetch_player_count("192.168.1.50", 27016))
Notes
- Different game engines (like Source vs. Unreal) might have minor differences in the response format—you can refer to Steam's official Server Query Protocol docs for precise parsing rules.
- Make sure your firewall allows outgoing UDP traffic to the server's query port.
Quick Tips
- Always handle rate limits: Steam's API has rate limits, so don't spam requests.
- For games with custom APIs (like CS:GO or Dota 2), check if they have dedicated endpoints for player counts—they might offer more detailed data.
内容的提问来源于stack exchange,提问作者alfredogoncalves




