|
Posted 10-4-2024 11:22:58
Determine the protocol used by the game server you want to query. This information can often be found in the game's documentation or community forums.
Use a UDP socket to connect to the query port of the game server. The query port is typically a well-known port number, such as 27015 for Source Engine games.
Send a query packet to the server, formatted according to the specific protocol. The packet typically includes information like the query type (e.g., "info", "players", "rules") and other parameters.
The server will respond with a packet containing the requested game stats. The format of the response will also depend on the protocol.
Example:
- <div>import socket</div><div>
- </div><div># Create a UDP socket</div><div>sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)</div><div>
- </div><div># Send a query packet</div><div>sock.sendto(b"info\0", ("gameserver.example.com", 27015))</div><div>
- </div><div># Receive a response</div><div>data, addr = sock.recvfrom(1024)</div><div>
- </div><div># Print the response</div><div>print(data.decode())</div><div>
- </div>
Copy the Code
|
|