Python: Establish Light Server to Share Command


I established a simple server and ask colleagues to test commands, on AWS EC2.

Story

In speech recognition, there’s a command to calculate some feature values from wav file and create new file. I prepared execution environment and enable to test the commands. But to share it to colleagues, I should prepare a sort of environment where the command can be easily evaluated.

Then, I established simple server with Python which is already installed. The server processes the controversial command and convert uploaded file and show output file on webpage.

Environment

  • AWS EC2
  • Python 2.7.6
  • OS: Ubuntu 64bit 14.04.3 LTS

Server

First, create directories and files as follows.

  • server_test/
    • cgi-bin/
      • test.py (chmod o+x)
    • tmp/ (chmod 777)

And write the following code to test.py.

The above code saves uploaded file as tmp/last.wav and execute the command, and read the output file, tmp/last.rec and show it on the webpage. It is simple code.

You can launch the server with the following command in server_test directory. It begins to listen TCP 80 port.

It is required to launch the server with sudo, because only super user can indicate to use 80 port.

Then you can view it on web browser.

The command written in the code is executed in server_test directory.

If you want to get standard output, the following code will help. Of course you can redirect standard output to a file and save it, and you can read from the file.

Thus, assign the output to rec, and you can show it on the web page likewise the previous code, print rec.