Source Remote Console (RCON)

The remote console (RCON) is available in all Source Dedicated Servers and it provides a way for server operators to access and administer their servers remotely. The valve.source.rcon module provides an implementation of the RCON protocol.

RCON is a request-response TCP based protocol with a simple authentication mechanism. The client initiates a connection with the server and attempts to authenticate by submitting a password. If authentication suceeds then the client is free to send further requests to the server in the same manner as you may do using the Source in-game console.

Warning

RCON does not use secure transport so the password is sent as plain text.

Note

Many RCON authentication failures in a row from a single host will result in the Source server automatically banning that IP, preventing any subsequent connection attempts.

Example

from valve.source.rcon import RCON

SERVER_ADDRESS = ("...", 27015)
PASSWORD = "top_secret"

with RCON(SERVER_ADDRESS, PASSWORD) as rcon:
    print(rcon("echo Hello, world!"))

In this example a RCON instance is created to connect to a Source RCON server, authenticating using the given password. Then the echo RCON command is issued which simply prints out what it receives.

Using the RCON object with the with statement means creation and clean up of the underlying TCP socket will happen automatically. Also, if the password is specified, the client will authenticate immediately after connecting.

The RCON Class

The RCON class implements the RCON client protocol. It supports the ability to finely grain transport creation, connection, authentication and clean up although its encouraged to make use of the with statement as shown in the example above.

class valve.source.rcon.RCON(address, password=None, timeout=10.0)
__call__(command)

Execute a command on the server

This wraps execute() but returns the response body instead of the request Message object.

__enter__()

Connect and optionally authenticate to the server

Authentication will only be attempted if the password attribute is set.

__exit__(exc_type, exc_value, exc_tb)

Disconnect from the server

__weakref__

list of weak references to the object (if defined)

authenticate(password)

Authenticates with the server using the given password.

Raises AuthenticationError if password is incorrect. Note that multiple attempts with the wrong password will result in the server automatically banning ‘this’ IP.

connect()

Connect to host, creating transport if necessary

execute(command, block=True)

Executes a SRCDS console command.

Returns the Message object that makes up the request sent to the server. If block is True, the response attribute will be set, unless a NoResposneError was raised whilst waiting for a response.

If block is False, calls must be made to process() until a response is recieved. E.g. use response_to().

Requires that the client is authenticated, otherwise an AuthenticationError is raised.

process()

Reads all avilable data from socket and attempts to process a response. Responses are automatically attached to their corresponding request.

request(type, body=u'')

Send a message to server.

If type is SEVERDATA_EXECCOMAND an addtional SERVERDATA_RESPONSE_VALUE is sent in order to facilitate correct processing of multi-packet responses.

response_to(request, timeout=None)

Returns a context manager that waits up to a given time for a response to a specific request. Assumes the request has actually been sent to an RCON server.

If the timeout period is exceeded, NoResponseError is raised.

RCON Messages

RCON requests and responses are generalised as messages in the python-valve implementation. If you’re using RCON.__call__() then you won’t need to worry about handling individual messages. However, RCON.execute() returns these raw messages so their structure is documented below.

class valve.source.rcon.Message(id, type, body=u'')
__weakref__

list of weak references to the object (if defined)

classmethod decode(buffer)

Will attempt to decode a single message from a byte buffer, returning a corresponding Message instance and the remaining buffer contents if any.

If buffer is does not contain at least one full message, IncompleteMessageError is raised.

encode()

Encode the message to a bytestring

Each packed message inludes the payload size (in bytes,) message ID and message type encoded into a 12 byte header. The header is followed by a null-terimnated ASCII-encoded string and a further trailing null terminator.

size

Packet size in bytes, minus the ‘size’ fields (4 bytes).

REPL via shell()

A small convenience function is provided by the valve.source.rcon module for creating command-line REPL interfaces for RCON connections.

valve.source.rcon.shell(rcon=None)