[Python] Query and RCON API (25/06/2012) -
StatusRed - 23.06.2012
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"
Re: [Python] Query and RCON API (23/06/2012) -
tyler12 - 23.06.2012
amazing
Re: [Python] Query and RCON API (23/06/2012) -
TTJJ - 23.06.2012
Works really good, brilliant.
Re: [Python] Query and RCON API (23/06/2012) -
StatusRed - 23.06.2012
Quote:
Originally Posted by tyler12
amazing
|
Quote:
Originally Posted by TTJJ
Works really good, brilliant.
|
Thanks.
Re: [Python] Query and RCON API (23/06/2012) -
ryansoper - 23.06.2012
Pretty Nifty tool.
Re: [Python] Query and RCON API (23/06/2012) -
Ronaldo_raul™ - 23.06.2012
I didn't got this ?
If I want to query my server details on my website, how can I use this ?
Or this is totally other thing according to what I am thinking right now ?
Re: [Python] Query and RCON API (23/06/2012) -
StatusRed - 23.06.2012
Quote:
Originally Posted by Ronaldo_raul™
I didn't got this ?
If I want to query my server details on my website, how can I use this ?
Or this is totally other thing according to what I am thinking right now ?
|
What language is your website using? Unless it's a Python / Django website it's not going to work.
Re: [Python] Query and RCON API (23/06/2012) -
Ronaldo_raul™ - 24.06.2012
Quote:
Originally Posted by StatusRed
What language is your website using? Unless it's a Python / Django website it's not going to work.
|
Okay, Thanks seems emma outta this as my site is not Python based
Re: [Python] Query and RCON API (23/06/2012) -
R@f - 24.06.2012
Nice release
Re: [Python] Query and RCON API (23/06/2012) -
Lordzy - 24.06.2012
Nice Release.
Gonna check it out!
Re: [Python] Query and RCON API (23/06/2012) -
StatusRed - 24.06.2012
I've found a bug which will cause a problem when querying servers with over 255 players online or with over 255 max slots.
I know exactly how to fix it, I'll do it shortly and update the post.
Re: [Python] Query and RCON API (23/06/2012) -
TheArcher - 24.06.2012
Nice release. keep it up.
Re: [Python] Query and RCON API (23/06/2012) -
StatusRed - 25.06.2012
Thanks for the appraisal guys.
I've just updated the top post for version 1.1; here's the change log:
Change log (1.0 -> 1.1):- Servers with over 255 online or max players will now display correctly
- Some fixes were made for RCON commands that were not working properly (such as sampRcon.reloadFS(fs) / sampRcon.reloadFilterscript(fs))
- Changed the copyright licensing
A Java API will be released later on today also.
Re: [Python] Query and RCON API (23/06/2012) -
StatusRed - 25.06.2012
Java API released:
https://sampforum.blast.hk/showthread.php?pid=1945763#pid1945763
Re: [Python] Query and RCON API (23/06/2012) -
mick88 - 18.04.2015
When I install this in my project's virtualenv using pip, it installs samples and readme.txt in my project's root directory. Is this intentional?
Re: [Python] Query and RCON API (23/06/2012) -
StatusRed - 10.06.2015
Quote:
Originally Posted by mick88
When I install this in my project's virtualenv using pip, it installs samples and readme.txt in my project's root directory. Is this intentional?
|
I believe it is. You should be able to delete the samples and readme with no problems though.
Re: [Python] Query and RCON API (23/06/2012) -
lexjusto - 28.06.2015
This module does not work on Python 3.4.3.
check this.
Re: [Python] Query and RCON API (23/06/2012) -
StatusRed - 12.07.2015
Quote:
Originally Posted by lexjusto
This module does not work on Python 3.4.3.
check this.
|
Hi,
Does it work in 2.7 however? Also, what about lower version of Python 3?
Thanks.
Re: [Python] Query and RCON API (23/06/2012) -
lexjusto - 13.07.2015
Quote:
Originally Posted by StatusRed
Hi,
Does it work in 2.7 however? Also, what about lower version of Python 3?
Thanks.
|
Yes, in 2.7 works, but in version 3.4 does not work, shows some errors, don't know about earlier versions 3.4.
Re: [Python] Query and RCON API (23/06/2012) -
StatusRed - 02.08.2015
Quote:
Originally Posted by lexjusto
Yes, in 2.7 works, but in version 3.4 does not work, shows some errors, don't know about earlier versions 3.4.
|
OK, well I know that 2.7 and 3.x Python versions are fundamentally different, I targeted this to work with Python 2.7, so I wouldn't expect it to work with 3.x to be honest. Sorry.