SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts > Tools and Files

Reply
 
Thread Tools Display Modes
Old 23/06/2012, 01:12 PM   #1
StatusRed
Big Clucker
 
Join Date: Jan 2011
Location: UK
Posts: 108
Reputation: 41
Default [Python] Query and RCON API (25/06/2012)

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:
Code:
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:
Code:
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:
Code:
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"

Last edited by StatusRed; 25/06/2012 at 02:53 PM.
StatusRed is offline   Reply With Quote
Old 23/06/2012, 01:22 PM   #2
tyler12
High-roller
 
tyler12's Avatar
 
Join Date: May 2011
Location: England
Posts: 1,016
Reputation: 239
Default Re: [Python] Query and RCON API (23/06/2012)

amazing
__________________
tyler12 is offline   Reply With Quote
Old 23/06/2012, 01:28 PM   #3
TTJJ
Big Clucker
 
Join Date: May 2007
Posts: 146
Reputation: 31
Default Re: [Python] Query and RCON API (23/06/2012)

Works really good, brilliant.
__________________
Soap Roleplay

My Releases:
[GAMEMODE] Just Roleplay
[FILTERSCRIPT] Flak Gun
TTJJ is offline   Reply With Quote
Old 23/06/2012, 01:31 PM   #4
StatusRed
Big Clucker
 
Join Date: Jan 2011
Location: UK
Posts: 108
Reputation: 41
Default Re: [Python] Query and RCON API (23/06/2012)

Quote:
Originally Posted by tyler12 View Post
amazing
Quote:
Originally Posted by TTJJ View Post
Works really good, brilliant.
Thanks.
StatusRed is offline   Reply With Quote
Old 23/06/2012, 02:45 PM   #5
ryansoper
Little Clucker
 
Join Date: Feb 2010
Posts: 17
Reputation: 10
Default Re: [Python] Query and RCON API (23/06/2012)

Pretty Nifty tool.
__________________

SOAP-RP Come join us! http://soap-rp.com/ |SAMP HOST: soap-rp.com:7777| TS: 85.236.100.27:30047
ryansoper is offline   Reply With Quote
Old 23/06/2012, 02:52 PM   #6
Ronaldo_raul™
Gangsta
 
Ronaldo_raul™'s Avatar
 
Join Date: Jun 2010
Location: .
Posts: 999
Reputation: 67
Default Re: [Python] Query and RCON API (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 ?
Ronaldo_raul™ is offline   Reply With Quote
Old 23/06/2012, 05:22 PM   #7
StatusRed
Big Clucker
 
Join Date: Jan 2011
Location: UK
Posts: 108
Reputation: 41
Default Re: [Python] Query and RCON API (23/06/2012)

Quote:
Originally Posted by Ronaldo_raul™ View Post
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.
StatusRed is offline   Reply With Quote
Old 24/06/2012, 01:12 PM   #8
Ronaldo_raul™
Gangsta
 
Ronaldo_raul™'s Avatar
 
Join Date: Jun 2010
Location: .
Posts: 999
Reputation: 67
Default Re: [Python] Query and RCON API (23/06/2012)

Quote:
Originally Posted by StatusRed View Post
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
Ronaldo_raul™ is offline   Reply With Quote
Old 24/06/2012, 04:01 PM   #10
Lordzy
High-roller
 
Lordzy's Avatar
 
Join Date: Mar 2012
Location: NetherRealm
Posts: 2,701
Reputation: 1177
Default Re: [Python] Query and RCON API (23/06/2012)

Nice Release.
Gonna check it out!
__________________
Currently inactive - I don't play at any SA-MP servers nor work on anything in PAWN for now. The projects that I've done so far in PAWN, which requires updates will be taking some time.
Lordzy is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Tool/Web/Other] [PHP] Server Query and RCON API - Be an admin at ease! (16/02/2010) Westie Tools and Files 178 28/03/2018 10:20 PM
[Tool/Web/Other] [Android]SA-MP App - Query and send RCON commands to your servers R@f Tools and Files 131 13/07/2017 11:31 PM
[Android]SA-MP App - Query and send RCON commands to your servers R@f Everything and Nothing 25 04/06/2012 11:35 PM
SAMP RCON & Query API CaTaLinU Scripting Help 5 11/02/2012 11:25 AM
Westies API / SAMP Api (Query & RCON) Lee Strong Help Archive 2 14/10/2010 09:37 AM


All times are GMT. The time now is 12:35 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.