SA-MP Query and RCON API for Python

Version: 1.1 (23/06/2012)

Download

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.

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(commanddelay=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"

1 thought on “SA-MP Query and RCON API for Python

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *

This site uses Akismet to reduce spam. Learn how your comment data is processed.