Page 1 of 1

Interactive python interpreter!

Posted: Sun Oct 04, 2015 12:07 am
by stonedegg
I've made a really small and simple plugin which lets you execute python code in the server/client console via server commands.
This can be very useful for plugin developers to quickly test some code.
Examples:

Enter the following in the server console or ingame in your client console with rcon in front:

Code: Select all

Will print "hello world!" to everyone on the server.
> SayText2("hello world!").send()

You can also use multiline code by using \n for a new line and \t for one indention:

Will print 1 2 3 4 in server console
> for x in range(1, 5):\n\tprint(x)

Will print 3 4 in server console
> for x in range(1, 5):\n\tif x > 2:\n\t\tprint(x)

Will print "hello world!" in chat with a previously defined variable
> msg = "hello world!"
> SayText2(msg).send()

Print a random number in the server console if its bigger than 50
> import random
> number = random.randint(1, 100)
> if number > 50: \n\tprint(number)\nelse: \n\tprint("number is", number)

Force a player to say something (get <userid> from "status" command)
> player = PlayerEntity(index_from_userid(<userid>))
> player.say("hello world!")



Use the server command ">!reload" to reload the interpreter and clear all imports/defined variables. Once you defined a variable or imported a module you will be able to use it for your whole "session", until you reload the interpreter.
You can automatically import modules by defining them in the imported_modules list in the script.


Syntax: Select all

import code

from listeners.tick import tick_delays
from commands.server import ServerCommand

imported_modules = [
'from players.entity import PlayerEntity',
'from players.helpers import index_from_userid',
'from messages import SayText2',

]

@ServerCommand('>!reload')
def reload_command(command):
global interpreter
interpreter = Interpreter()
load_info(True)

@ServerCommand('>')
def interprete_command(command):
c = command.get_arg_string()
c = c.replace('\\n', '\n').replace('\\t', '\t')
if c:
interpreter.execute(c)

class Interpreter(code.InteractiveInterpreter):
def __init__(self):
super(Interpreter, self).__init__()

def execute(self, c):
print("\n[SP] Interactive Interpreter executing: \n\n" + c + "\n")
self.runcode(c)


interpreter = Interpreter()

def load():
tick_delays.delay(0.1, load_info)

def load_info(reloaded=False):
if reloaded:
print("\n[SP] Interactive Interpreter reloaded\n")
else:
print("\n[SP] Interactive Interpreter loaded\n")

if len(imported_modules):
print("Importing modules:")
for module in imported_modules:
print(" " + module)
interpreter.runcode(module)
print("\n")

Posted: Sun Oct 04, 2015 8:39 am
by Ayuto
Nice! If you have direct access to the server console, it's also possible to completely start an interactive session in the server console. Maybe you also want to add a command to start such a session.

Posted: Mon Oct 05, 2015 12:18 am
by stonedegg
Yea I tested that, but it freezes the server. I mainly created this to interactively test code using the SP api.

Posted: Mon Oct 05, 2015 8:35 am
by Predz
You could create 1 line plugins with this xD

Challenge accepted :D

Posted: Mon Oct 05, 2015 9:37 am
by Ayuto
Predz wrote:You could create 1 line plugins with this xD

Challenge accepted :D
Done! :D

http://forums.eventscripts.com/viewtopic.php?f=40&t=45881&p=400217#p400217

stonedegg wrote:Yea I tested that, but it freezes the server. I mainly created this to interactively test code using the SP api.
GODJonez made an ES addon that didn't freeze the server. You might want to take a look at it.

Posted: Mon Oct 05, 2015 10:56 am
by stonedegg
Ayuto wrote:GODJonez made an ES addon that didn't freeze the server. You might want to take a look at it.


Uh, that looks like a tough task, especially cause I've never done anything with sockets before in python. I'll might give it a try once I got more time, but for now this one should serve. :)

Re: Interactive python interpreter!

Posted: Sun Oct 15, 2017 6:22 pm
by rocco
FYI - tick_delays is no longer supported so this interpreter is broken.

See viewtopic.php?t=1036