[Tool/Web/Other] [Python] Query and RCON API (23/06/2012)
#1

SA-MP Query and RCON API for Python
Edward McKnight (EM-Creations.co.uk) - StatusRed
Version: 1.1 (25/06/2012)
Python Version: 2.7.2
Due to working in the Django (Python) web framework recently, I decided to create a Python SA-MP Query and RCON API. After many frustrating hours here it is.

As well as the examples here; included in the download are sampRconExample.py and sampQueryExample.py files. These are full of examples of how to call functions as well as documentation.

There's no reason why these classes shouldn't work in regular Python programs as well as Django (Python Web Framework).

Feel free to post if you have any questions or problems.

sampQuery functions:
sampQuery.new(server="127.0.0.1", port=7777)
sampQuery.connect()
sampQuery.close()
sampQuery.getInfo()
sampQuery.getBasicPlayers()
sampQuery.getDetailedPlayers()
sampQuery.getRules()

Example | Output server info and basic players:
Код:
import sampQuery

print "Starting program.."
query = sampQuery.new("127.0.0.1", 7777) # The server parameter can also take strings such as: server.101stdivision.net

print "Trying to connect.."
if query.connect(): # If the program has made a successful connection to the server
    print "Info:"
    serverInfo = query.getInfo()

    if serverInfo['password']:
        passworded = "True"
    else:
        passworded = "False"
    print serverInfo['hostname']+" - Password: "+passworded+" Players: "+str(serverInfo['players'])+"/"+str(serverInfo['maxplayers'])+" Map: "+serverInfo['map']+" Gamemode: "+serverInfo['gamemode']


    basicPlayers = query.getBasicPlayers() # Returns False if no players are online

    if basicPlayers: # If the server has players online
        print "Basic Players:"
        for player in basicPlayers:
            print str(player['name'])+" "+str(player['score'])
    else:
        print "No players"

print "Closing connection.."
    query.close() # Close the connection
else:
    print "Server didn't respond"
sampRcon functions:
sampRcon.new(server="127.0.0.1", port=7777, password="changeme")
sampRcon.connect()
sampRcon.close()
sampRcon.getCommandList()
sampRcon.getServerVariables()
sampRcon.setWeather(weatherID=1)
sampRcon.setGravity(gravity=0.008)
sampRcon.ban(playerID)
sampRcon.kick(playerID)
sampRcon.banAddress(address)
sampRcon.unbanAddress(address)
sampRcon.reloadLog()
sampRcon.reloadBans()
sampRcon.say(message)
sampRcon.changeGameMode(gamemode)
sampRcon.nextGameMode()
sampRcon.gmx() {same as sampRcon.nextGameMode()}
sampRcon.execConfig(config)
sampRcon.loadFilterscript(fs)
sampRcon.loadFS(fs) {same as sampRcon.loadFilterscript(fs)}
sampRcon.unloadFilterscript(fs)
sampRcon.unloadFS(fs) {same as sampRcon.unloadFilterscript(fs)}
sampRcon.reloadFilterscript(fs)
sampRcon.reloadFS(fs) {same as sampRcon.reloadFilterscript(fs)}
sampRcon.exit()
sampRcon.call(command, delay=False)

Example | Output command list:
Код:
import sampRcon

# Note: You're likely to get a socket.timeout exception if you have provided the incorrect rcon password
print "Starting program.."
query = sampRcon.new("127.0.0.1", 7777, "changeme1")

if query.connect(): # If the program has made a successful connection to the server
    commands = query.getCommandList() # Note commands[0] will contain: "Console Commands::", and not an actual server variable

    for command in commands:
        print command
    query.close() # Close the connection
else:
    print "Server didn't respond"
Example | Ban player:
Код:
import sampRcon

# Note: You're likely to get a socket.timeout exception if you have provided the incorrect rcon password
print "Starting program.."
query = sampRcon.new("127.0.0.1", 7777, "changeme1")

if query.connect(): # If the program has made a successful connection to the server
    query.ban(4) # Ban ID 4
    query.reloadBans() # Reload the bans file
    query.close() # Close the connection
else:
    print "Server didn't respond"
Reply


Messages In This Thread
[Python] Query and RCON API (25/06/2012) - by StatusRed - 23.06.2012, 13:12
Re: [Python] Query and RCON API (23/06/2012) - by tyler12 - 23.06.2012, 13:22
Re: [Python] Query and RCON API (23/06/2012) - by TTJJ - 23.06.2012, 13:28
Re: [Python] Query and RCON API (23/06/2012) - by StatusRed - 23.06.2012, 13:31
Re: [Python] Query and RCON API (23/06/2012) - by ryansoper - 23.06.2012, 14:45
Re: [Python] Query and RCON API (23/06/2012) - by Ronaldo_raul™ - 23.06.2012, 14:52
Re: [Python] Query and RCON API (23/06/2012) - by StatusRed - 23.06.2012, 17:22
Re: [Python] Query and RCON API (23/06/2012) - by Ronaldo_raul™ - 24.06.2012, 13:12
Re: [Python] Query and RCON API (23/06/2012) - by R@f - 24.06.2012, 15:56
Re: [Python] Query and RCON API (23/06/2012) - by Lordzy - 24.06.2012, 16:01
Re: [Python] Query and RCON API (23/06/2012) - by StatusRed - 24.06.2012, 18:31
Re: [Python] Query and RCON API (23/06/2012) - by TheArcher - 24.06.2012, 18:52
Re: [Python] Query and RCON API (23/06/2012) - by StatusRed - 25.06.2012, 11:45
Re: [Python] Query and RCON API (23/06/2012) - by StatusRed - 25.06.2012, 15:27
Re: [Python] Query and RCON API (23/06/2012) - by mick88 - 18.04.2015, 14:27
Re: [Python] Query and RCON API (23/06/2012) - by StatusRed - 10.06.2015, 19:57
Re: [Python] Query and RCON API (23/06/2012) - by lexjusto - 28.06.2015, 11:34
Re: [Python] Query and RCON API (23/06/2012) - by StatusRed - 12.07.2015, 06:31
Re: [Python] Query and RCON API (23/06/2012) - by lexjusto - 13.07.2015, 01:50
Re: [Python] Query and RCON API (23/06/2012) - by StatusRed - 02.08.2015, 16:29
Re: [Python] Query and RCON API (23/06/2012) - by Knight_Rider - 02.08.2015, 16:46
Knight_Rider - by StatusRed - 11.02.2017, 17:02
Re: [Python] Query and RCON API (23/06/2012) - by mick88 - 02.07.2017, 20:49

Forum Jump:


Users browsing this thread: 2 Guest(s)