Home > Uncategorized > Automate Python code execution with predefined I/O

Automate Python code execution with predefined I/O

Need to not prompt for input in program execution and store output in a variable.

If you need a program that can arbitrarily execute a piece of Python code, and get the result, then this post is made for you. Much like in a programming contest. It’s not complicated if you need this kind of functionality on a Web Server that run and breath Python- it was the Django framework for my case.

As it turns out, you can actually run a piece of code embedded inside a String much like in javascript. And thats pretty damn cool, Sir. Here’s how to do it… if you want to understand the parameters, well you don’t say! Look elsewhere :-).

code = compile(newCode, '<string>', 'exec')

newCode is a string (can be single line or multiline string- those inside ”’ statement ”’ that span multiple line). Yes, Python can compile code! I bet you didn’t know that :P. Be careful when executing user submitted python code like this. It won’t be long before a filthy user run malicious code to wreck havoc on your server. You can run it in a sandbox, but that is very complex to do. You can analyze the code and check for imports that may be potentially harmful (that is what i did, like a boss).

You will need to execute it as follows

exec code

Oh wait..i am running this on a server.. How do i get the output? How to put the input? What follows below will be a bit hackish 😛

Well what you need is write values directly to sys.stdin = This is where when you type in the keyboard will be stored. sys.stdout is where the print get buffered. You will need to get it before it get lost.

Here is the code:

import sys
from StringIO import StringIO

code = compile(newCode, '<string>', 'exec')

# Store the reference, for later reference
old_stdout = sys.stdout

# variable that will store the output stream
result = StringIO()
sys.stdout = result

# variable that will store the input stream value
sinput = StringIO("ENTER INPUT VALUE")
sys.stdin = sinput

# Run code
exec code

# Redirect again the std output to screen
sys.stdout = old_stdout

# Then, get the stdout like a string(or boss)!
result_string = result.getvalue()

Now you have the output in result_string and can do whatever you want from it.

So here’s one approach Python geeks and freaks can use for automating a program execution. Alternatively, you could run doctest and/or unitest(boring -_-).

Advertisements
Categories: Uncategorized
  1. nalla
    March 15, 2012 at 9:17 pm

    mission accomplished 😛

  2. 0x1337
    March 16, 2012 at 9:07 pm

    imba :O

    • March 18, 2012 at 4:58 pm

      tw ki imba 😛
      welcome boss \m/ 🙂

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: