[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
#2

amazing
Reply
#3

Works really good, brilliant.
Reply
#4

Quote:
Originally Posted by tyler12
Посмотреть сообщение
amazing
Quote:
Originally Posted by TTJJ
Посмотреть сообщение
Works really good, brilliant.
Thanks.
Reply
#5

Pretty Nifty tool.
Reply
#6

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 ?
Reply
#7

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.
Reply
#8

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
Reply
#9

Nice release
Reply
#10

Nice Release.
Gonna check it out!
Reply
#11

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.
Reply
#12

Nice release. keep it up.
Reply
#13

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.
Reply
#14

Java API released: https://sampforum.blast.hk/showthread.php?pid=1945763#pid1945763
Reply
#15

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?
Reply
#16

Quote:
Originally Posted by mick88
View Post
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.
Reply
#17

This module does not work on Python 3.4.3.

check this.
Reply
#18

Quote:
Originally Posted by lexjusto
View Post
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.
Reply
#19

Quote:
Originally Posted by StatusRed
View Post
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.
Reply
#20

Quote:
Originally Posted by lexjusto
View Post
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.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)