Skip to main content

SAMP.py

Downloads

SAMP.py is a GTA SA:MP RCON and query client for Python with full async/await support.

Note: As of the latest version, all client methods are asynchronous. Use async with and await throughout your code.


Installation

pip install samp-python

Requirements

  • Python 3.8+

Usage

All interactions with SampClient are now completely asynchronous. Use async with to manage the connection and await every method call.

Basic query

import asyncio
from samp_py.client import SampClient

async def main():
    async with SampClient(address='localhost', port=7777) as client:
        info = await client.get_server_info()
        print(info)

asyncio.run(main())

Running RCON commands

import asyncio
from samp_py.client import SampClient

async def main():
    async with SampClient(address='localhost', port=7777, rcon_password='password') as client:
        await client.rcon_cmdlist()

asyncio.run(main())

Accessing query and RCON responses

import asyncio
from samp_py.client import SampClient

async def main():
    async with SampClient(address='localhost', port=7777, rcon_password='password') as client:
        info = await client.get_server_info()
        print(info)
        print(info.gamemode)
        print(await client.rcon_get_hostname())
        players = await client.rcon_players()
        print(players[0].ping)

asyncio.run(main())

Exceptions

Exception Description
SampError General SA:MP error
RconError RCON is unavailable or password not provided
InvalidRconPassword The RCON password is incorrect
ConnectionError Could not connect to the server

Full Example

Below is a complete interactive SAMP.py script updated for async:

import sys
import asyncio
from builtins import input
from samp_py.client import SampClient


async def info(client):
    server_info = await client.get_server_info()
    print("""Server Info:
Password: {info.password}
Players: {info.players}/{info.max_players}
Hostname: {info.hostname}
Gamemode: {info.gamemode}
Language: {info.language}
    """.format(info=server_info))


async def rules(client):
    print("Server Rules:")
    for rule in await client.get_server_rules():
        print("{rule.name}: {rule.value}".format(rule=rule))


async def clients(client):
    print("Connected Clients")
    print("Name                       | Score")
    print("==================================")
    for c in await client.get_server_clients():
        print("{client.name:26} | {client.score}".format(client=c))


async def details(client):
    print("Detailed Clients")
    print(" ID | Name                       | Score | Ping")
    print("================================================")
    for c in await client.get_server_clients_detailed():
        print("{client.id:3} | {client.name:26} | {client.score:5} | {client.ping:4}".format(client=c))


async def rcon(client):
    if client.rcon_password is None:
        client.rcon_password = input('RCON password: ')
    print('Enter rcon commands or leave blank to exit. Example: cmdlist')
    while True:
        command = input('RCON: ')
        if not command:
            return
        for line in await client.send_rcon_command(command):
            print(line)


async def main(args):
    async with SampClient(*args) as client:
        if not await client.is_online():
            print('Server {}:{} is offline'.format(*args))
            exit(1)

        server_info = await client.get_server_info()
        print("""Connected to {info.hostname}.
Currently {info.players}/{info.max_players} players online.
Select one of the options:
i. Server Info
r. Server Rules
c. Connected clients
d. Detailed clients
x. RCON
""".format(info=server_info))

        options = {
            'i': info,
            'r': rules,
            'c': clients,
            'd': details,
            'x': rcon,
        }

        option = input('Select option: ')
        if option in options:
            await options[option](client)
        else:
            print('Unknown option, bye!')


if len(sys.argv) < 3:
    print('Usage: py example.py [server_address] [port]')
    exit(1)

asyncio.run(main(sys.argv[1:]))

API Reference

SampClient(address, port, rcon_password)

Parameter Type Default Description
address str '127.0.0.1' Server hostname or IP address
port int 7777 Server port
rcon_password str None RCON password (required for RCON)

Connection

Method Description
await client.connect() Connect to the server (called by async with)
client.disconnect() Close the connection

Query Methods

Method Returns
await client.get_server_info() ServerInfo
await client.get_server_rules() list[Rule]
await client.get_server_rules_dict() dict
await client.get_server_clients() list[Client]
await client.get_server_clients_detailed() list[ClientDetail]
await client.is_online() bool
await client.probe_server(value) bytes

RCON Methods

Method Description
await client.rcon_cmdlist() List available RCON commands
await client.rcon_varlist() List server variables
await client.rcon_varlist_dict() Server variables as a dict
await client.rcon_players() List connected players
await client.rcon_kick(player_id) Kick a player by ID
await client.rcon_ban(player_id) Ban a player by ID
await client.rcon_banip(ip_address) Ban an IP address
await client.rcon_unbanip(ip_address) Unban an IP address
await client.rcon_say(message) Broadcast a message
await client.rcon_echo(text) Echo text to the server console
await client.rcon_exec(filename) Execute a server config file
await client.rcon_changemode(mode) Change the game mode
await client.rcon_gmx() Restart the game mode
await client.rcon_gravity(gravity) Set server gravity
await client.rcon_weather(weather) Set server weather
await client.rcon_loadfs(name) Load a filterscript
await client.rcon_unloadfs(name) Unload a filterscript
await client.rcon_reloadfs(name) Reload a filterscript
await client.rcon_get_hostname() Get the server hostname
await client.rcon_set_hostname(name) Set the server hostname
await client.rcon_get_language() Get the server language
await client.rcon_set_language(language) Set the server language
await client.rcon_get_password() Get the server password
await client.rcon_set_password(password) Set the server password
await client.rcon_get_rcon_password() Get the RCON password
await client.rcon_set_rcon_password(password) Set the RCON password
await client.rcon_exit() Shut down the server
await client.send_rcon_command(command) Send a raw RCON command

Metadata

Release files for samp-python 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for samp-python 2.0.0
File Size Uploaded Provenance Actions
samp_python-2.0.0.tar.gz 21.5 kB Without provenance Details for samp_python-2.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for samp-python 2.0.0
File Interpreter ABI Platform Actions
samp_python-2.0.0-py3-none-any.whl Python 3 none any Details for samp_python-2.0.0-py3-none-any.whl

Total release size: 43.3 kB

Release files / samp_python-2.0.0.tar.gz

Download URL samp_python-2.0.0.tar.gz
Size 21.5 kB
Tags Source
SHA-256 checksum
How to use checksums
1fe162069b79965a136d3282ec1c1db7ced3012cd18b0112c07e6b12be5309ef
BLAKE2b-256 checksum
How to use checksums
190ced30e6510786e5fa8f6b7ed0db5379f658e240f7059979eb8cf367d87e22
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Release files / samp_python-2.0.0-py3-none-any.whl

Download URL samp_python-2.0.0-py3-none-any.whl
Size 21.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eee52e44bb5d3ff42a3f16206e2478d094752276ae2f2d6328a756b110e950f4
BLAKE2b-256 checksum
How to use checksums
5dd37f0f8b9654dee97c81845d91e47db7ea9494e5ec6b31782de438f642f613
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Security for samp-python 2.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page