Wednesday, April 02, 2008

Web Service

Bash Curses Cancer recently posted an article, Exposing Command Line Programs As Web Services, which I found it both interesting and useful. The web service web server script, to_web.py, is written in Python. It contains only 53 lines of code excluding blank lines. The example in the web site showed how you can expose "sort" and "gzip" to make wonder. Since "sort" and "gzip" are mostly available in most of the UNIXes, I think the concept can be adopted so that we can expose some "heavy weight" command line application as web service.

Octave immediately came to my mind. Quoted from the Octave web site: "GNU Octave is a high-level language, primarily intended for numerical computations. It provides a convenient command line interface for solving linear and nonlinear problems numerically, and for performing other numerical experiments using a language that is mostly compatible with Matlab. It may also be used as a batch-oriented language.".

Let's start up the web service server on my Cygwin

$ python -V
Python 2.5.1

$ uname -a
CYGWIN_NT-5.1 CHCHAN 1.5.25(0.156/4/2) 2007-12-14 19:21 i686 Cygwin

$ ./to_web.py -p8000 octave
Tue Apr  1 23:07:14 2008 octave server started - 8000
localhost - - [01/Apr/2008 23:07:37] "POST /octave+-q HTTP/1.1" 200 -
localhost - - [01/Apr/2008 23:08:12] "POST /octave+-q HTTP/1.1" 200 -
localhost - - [01/Apr/2008 23:08:50] "POST /octave+-q HTTP/1.1" 200 -
localhost - - [01/Apr/2008 23:08:57] "POST /octave+-q HTTP/1.1" 200 -

Below showed a couple of examples.

  1. Inverse of a 3x3 matrix
  2. Solving a set of equations: 2x+3y+4z=1 ; 3x+2y+z=2 ; 4x+y+3z = 3
  3. Compute product of a lower and upper triangular matrix based on LU Decomposition
  4. Find a 5x5 Magic Square
$ echo "a=[2,3,4;3,2,1;4,1,3];inv(a)" | curl --data-binary @- http://localhost:8000/octave+-q
ans =

-0.20000   0.20000   0.20000
0.20000   0.40000  -0.40000
0.20000  -0.40000   0.20000


$ echo "a=[2,3,4;3,2,1;4,1,3];b=[1;2;3];inv(a)*b" | curl --data-binary @- http://localhost:8000/octave+-q
ans =

 8.0000e-01
-2.0000e-01
-5.5511e-17


$ echo "[l,u]=lu(magic(5))" | curl --data-binary @- http://localhost:8000/octave+-q
l =

0.73913  1.00000  0.00000  0.00000  0.00000
1.00000  0.00000  0.00000  0.00000  0.00000
0.17391  0.25268  0.51637  1.00000  0.00000
0.43478  0.48394  0.72308  0.92308  1.00000
0.47826  0.76874  1.00000  0.00000  0.00000

u =

23.00000    5.00000    7.00000   14.00000   16.00000
 0.00000   20.30435   -4.17391   -2.34783    3.17391
 0.00000    0.00000   24.86081   -2.89079   -1.09208
 0.00000    0.00000    0.00000   19.65116   18.97933
 0.00000    0.00000    0.00000    0.00000  -22.22222


$ echo "magic(5)" | curl --data-binary @- http://localhost:8000/octave+-q
ans =

17  24   1   8  15
23   5   7  14  16
4   6  13  20  22
10  12  19  21   3
11  18  25   2   9

I think the possibility is endless. BTW, I started writing my own version based on Tcl and gotten the HTTP GET working. Have to find time to work on the HTTP POST. Python comes with BaseHTTPServer module that is pretty handy to build simple web server type of applications. Although Tcl has it own Tclhttpd web server, it is almost a full-blown standalone web server. Anyway, my version will be based on Tcl socket and I will try to achieve the same functionalities under 100 lines of code. So stay tune.

Labels: , ,

0 Comments:

Post a Comment

<< Home