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
)
- cgi-bin/
And write the following code to test.py
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
#!/usr/bin/python # -*- coding: utf-8 -*- import cgi import os form = cgi.FieldStorage() print "Content-type: text/html" print print "<!DOCTYPE html>" print "<html lang="ja">" print "<meta name="viewport" content="initial-scale=1.0" />" print "<head>" print "</head>" print "<body>" if "wav" in form: waveFile = form["wav"] if waveFile.file: # save file fout = file("tmp/last.wav", "wb") while True: chunk = waveFile.file.read(1024 * 1024 * 10) if not chunk: break fout.write(chunk) fout.close() # calculate os.system("some_command -i tmp/last.wav") # read output fin = file("tmp/last.rec", "r") rec = fin.read() fin.close() # format and show print "<pre>" print rec print "</pre>" print "<form method="post" enctype="multipart/form-data">" print "<input type="file" name="wav" />" print "<button>Submit</button>" print "</form>" print "</body>" print "</html>" |
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.
1 |
sudo python -m CGIHTTPServer 80 |
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.
1 2 3 4 5 6 |
import subprocess p = subprocess.Popen( "some command", stdout=subprocess.PIPE, shell=True) (rec, err) = p.communicate() |
Thus, assign the output to rec, and you can show it on the web page likewise the previous code, print rec
.