samp-python


Namesamp-python JSON
Version 1.0.0 PyPI version JSON
download
home_pagehttps://github.com/enginestein/SAMP.py
SummarySA:MP API client for Python
upload_time2023-08-16 04:34:24
maintainer
docs_urlNone
authorArya Praneil Pritesh
requires_python
licenseGPL-3.0-only
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # SAMP.py

SAMP.py is a GTA SA:MP RCON and query client for Python.

# Installation

```bash
pip install samp-py
```

# Usage

You can initalize a client like this:

```py
from samp_py.client import SampClient

with SampClient(address='localhost', port=7777) as client:
    print(client.get_server_info())
```

To run RCON commands:

```py
with SampClient(address='localhost', port=7777, rcon_password='password') as client:
    client.rcon_cmdlist()
```

Query and RCON responses can be accessed following the native route:

```py
with SampClient(address='localhost', port=7777, rcon_password='password') as client:
    info = client.get_server_info()
    print(info)
    print(info.gamemode)
    print(client.rcon_get_hostname())
    print(client.rcon_players()[0].ping)
```

There are not many exceptions:

- SampError
- RconError
- InvalidRconPassword
- ConnectionError

Here is an whole example of a SAMP.py script

```py
import sys
from builtins import input

from samp_py.client import SampClient


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


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


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


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


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 client.send_rcon_command(command):
            print(line)


def main(args):
    with SampClient(*args) as client:
        if not client.is_online():
            print('Server {}:{} is offline'.format(*args))
            exit(1)
        server_info = 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: options[option](client)
        else: print('Unknown option, bye!')


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

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/enginestein/SAMP.py",
    "name": "samp-python",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Arya Praneil Pritesh",
    "author_email": "aryapraneil@gmail.com",
    "download_url": "",
    "platform": null,
    "description": "# SAMP.py\r\n\r\nSAMP.py is a GTA SA:MP RCON and query client for Python.\r\n\r\n# Installation\r\n\r\n```bash\r\npip install samp-py\r\n```\r\n\r\n# Usage\r\n\r\nYou can initalize a client like this:\r\n\r\n```py\r\nfrom samp_py.client import SampClient\r\n\r\nwith SampClient(address='localhost', port=7777) as client:\r\n    print(client.get_server_info())\r\n```\r\n\r\nTo run RCON commands:\r\n\r\n```py\r\nwith SampClient(address='localhost', port=7777, rcon_password='password') as client:\r\n    client.rcon_cmdlist()\r\n```\r\n\r\nQuery and RCON responses can be accessed following the native route:\r\n\r\n```py\r\nwith SampClient(address='localhost', port=7777, rcon_password='password') as client:\r\n    info = client.get_server_info()\r\n    print(info)\r\n    print(info.gamemode)\r\n    print(client.rcon_get_hostname())\r\n    print(client.rcon_players()[0].ping)\r\n```\r\n\r\nThere are not many exceptions:\r\n\r\n- SampError\r\n- RconError\r\n- InvalidRconPassword\r\n- ConnectionError\r\n\r\nHere is an whole example of a SAMP.py script\r\n\r\n```py\r\nimport sys\r\nfrom builtins import input\r\n\r\nfrom samp_py.client import SampClient\r\n\r\n\r\ndef info(client):\r\n    print(\"\"\"Server Info:\r\nPassword: {info.password}\r\nPlayers: {info.players}/{info.max_players}\r\nHostname: {info.hostname}\r\nGamemode: {info.gamemode}\r\nLanguage: {info.language}\r\n    \"\"\".format(\r\n        info=client.get_server_info(),\r\n    ))\r\n\r\n\r\ndef rules(client):\r\n    print(\"Server Rules:\")\r\n    for rule in client.get_server_rules():\r\n        print(\"{rule.name}: {rule.value}\".format(\r\n            rule=rule,\r\n        ))\r\n\r\n\r\ndef clients(client):\r\n    print(\"Connected Clients\")\r\n    print(\"Name                       | Score\".format(client=client))\r\n    print(\"==================================\")\r\n    for client in client.get_server_clients():\r\n        print(\"{client.name:26} | {client.score}\".format(client=client))\r\n\r\n\r\ndef details(client):\r\n    print(\"Detailed Clients\")\r\n    print(\" ID |Name                        | Score | Ping\".format(client=client))\r\n    print(\"===============================================\")\r\n    for client in client.get_server_clients_detailed():\r\n        print(\"{client.id:3} | {client.name:26} | {client.score:5} | {client.ping:4}\".format(client=client))\r\n\r\n\r\ndef rcon(client):\r\n    if client.rcon_password is None:\r\n        client.rcon_password = input('RCON password:')\r\n    print('Enter rcon commands or leave blank to exit. Example: cmdlist')\r\n    while True:\r\n        command = input('RCON: ')\r\n        if not command:\r\n            return\r\n        for line in client.send_rcon_command(command):\r\n            print(line)\r\n\r\n\r\ndef main(args):\r\n    with SampClient(*args) as client:\r\n        if not client.is_online():\r\n            print('Server {}:{} is offline'.format(*args))\r\n            exit(1)\r\n        server_info = client.get_server_info()\r\n        print(\"\"\"Connected to {info.hostname}. \r\nCurrently {info.players}/{info.max_players} players online.\r\n\r\nSelect one of the options:\r\n\r\ni. Server Info\r\nr. Server Rules\r\nc. Connected clients\r\nd. Detailed clients\r\nx. RCON\r\n\"\"\".format(\r\n            info=server_info,\r\n        ))\r\n\r\n        options = {\r\n            'i': info,\r\n            'r': rules,\r\n            'c': clients,\r\n            'd': details,\r\n            'x': rcon,\r\n        }\r\n        option = input('Select option: ')\r\n        if option in options: options[option](client)\r\n        else: print('Unknown option, bye!')\r\n\r\n\r\nif len(sys.argv) < 3:\r\n    print('Usage: py example.py [server_address] [port]')\r\n    exit(1)\r\nmain(sys.argv[1:])\r\n```\r\n",
    "bugtrack_url": null,
    "license": "GPL-3.0-only",
    "summary": "SA:MP API client for Python",
    "version": "1.0.0",
    "project_urls": {
        "Homepage": "https://github.com/enginestein/SAMP.py",
        "Source": "https://github.com/enginestein/SAMP.py",
        "Tracker": "https://github.com/enginestein/SAMP.py"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "434b67f4415f8b5b7ca728f234235a90c5e6ec06fd457662469e7a7d9456422d",
                "md5": "d0833e276024aa488a4e8b38403654dc",
                "sha256": "2bc85d9cdc2bbc3a4d08000fd268e3c0f45b7bd3f2cbf8f86d443eb881927c88"
            },
            "downloads": -1,
            "filename": "samp_python-1.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d0833e276024aa488a4e8b38403654dc",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 8138,
            "upload_time": "2023-08-16T04:34:24",
            "upload_time_iso_8601": "2023-08-16T04:34:24.183273Z",
            "url": "https://files.pythonhosted.org/packages/43/4b/67f4415f8b5b7ca728f234235a90c5e6ec06fd457662469e7a7d9456422d/samp_python-1.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-16 04:34:24",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "enginestein",
    "github_project": "SAMP.py",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "samp-python"
}
        
Elapsed time: 1.87499s