Skip to content

Tutorial 06 Web Scripter

wavesoft edited this page Oct 26, 2014 · 3 revisions

In this chapter you are going to apply what you learned on the previous ones and build your own browser-based python scripting interface.

Components for your assistance

On the second chapter, where we built the server.py we configured it to start a specific CernVM image that contains a script to assist you with this purpose.

A script called runscript.php is accessible as http://<API Endpoint>/runscript.php. This script accepts POST requests with the following parameters:

http://<API Endpoint>/runscript.php?int=<interpreter>&t=<timeout>

Where:

  • int is the full path to the interpreter you want to use (defaults to /usr/bin/python)
  • t is the timeout (in seconds) before the script kills the running process

The script will reply with a JSON response:

{
    "result"    : "ok", // Or 'error' if something whent wrong
    "error"     : "",   // The error description
    "stdout"    : "",   // The standard output from the script
    "stderr"    : "",   // The dandard error output from the script
    "return"    : 0,    // The exit code of the interpreter
}

Copy-pastable script for the lazy ones:

// I assume you predefine this variable somewhere
var vmcpURL = "";

// Run a script
function run_script(script, callback, interpreter, timeout) {
    var int = interpreter || "/usr/bin/python",
        t = timeout || 30;
    $.ajax({
      type: "POST",
      url: vmcpURL + "/runscript.php?int="+int+"&t="+t,
      data: script,
      dataType: 'json',
      success: callback
    });
}

The first parameter to the callback function is an object with the parsed JSON response.

Todo checklist

  1. Create a simple page that opens a session to a scripting VM upon startup
  2. Create a 'Start' button that is going to start the VM
  3. Create two text areas and two buttons, 'Run' and 'Stop'
  4. 'Run' will run the script written in the first text area and will dump the standard output to the other text area
  5. 'Stop' will stop the VM

Ready, set, go!