23.06.2012, 13:12
(
Последний раз редактировалось StatusRed; 25.06.2012 в 14:53.
)
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.Edward McKnight (EM-Creations.co.uk) - StatusRed
Version: 1.1 (25/06/2012)
Python Version: 2.7.2
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.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"
Код:
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"