Interactive python interpreter!

Post Python examples to help other users.
stonedegg
Senior Member
Posts: 141
Joined: Sun Aug 12, 2012 11:45 am

Interactive python interpreter!

Postby stonedegg » Sun Oct 04, 2015 12:07 am

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")
User avatar
Ayuto
Project Leader
Posts: 2197
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Sun Oct 04, 2015 8:39 am

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.
stonedegg
Senior Member
Posts: 141
Joined: Sun Aug 12, 2012 11:45 am

Postby stonedegg » Mon Oct 05, 2015 12:18 am

Yea I tested that, but it freezes the server. I mainly created this to interactively test code using the SP api.
Predz
Senior Member
Posts: 158
Joined: Wed Aug 08, 2012 9:05 pm
Location: Bristol, United Kingdom

Postby Predz » Mon Oct 05, 2015 8:35 am

You could create 1 line plugins with this xD

Challenge accepted :D
User avatar
Ayuto
Project Leader
Posts: 2197
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Mon Oct 05, 2015 9:37 am

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.
stonedegg
Senior Member
Posts: 141
Joined: Sun Aug 12, 2012 11:45 am

Postby stonedegg » Mon Oct 05, 2015 10:56 am

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. :)
User avatar
rocco
Junior Member
Posts: 5
Joined: Thu Jun 29, 2017 5:56 am

Re: Interactive python interpreter!

Postby rocco » Sun Oct 15, 2017 6:22 pm

FYI - tick_delays is no longer supported so this interpreter is broken.

See viewtopic.php?t=1036

Return to “Code examples / Cookbook”

Who is online

Users browsing this forum: No registered users and 3 guests