PythonにはCGIHTTPServerというクラスがあって、これが便利ですよっと。
20.20. CGIHTTPServer — CGI 実行機能付き HTTP リクエスト処理機構 — Python 2.6ja2 documentation
>python -m CGIHTTPServer Serving HTTP on 0.0.0.0 port 8000 ...
WindowsだとCtrl+Breakで終了できます。
コマンド一発でCGIが動作するHTTPサーバが起動します。デフォルトでは "/cgi-bin/"、"/htbin" 以下でCGIが動作します。
カレントディレクトリがドキュメントルートになります。
試しに簡単なCGIを書いてみる*1。
hello.c
#include<stdio.h> int main(void) { printf("Content-Type: text/html\n\n"); printf("<html><head>"); printf("<title>Hello world CGI</title>"); printf("</head><body>"); printf("<h1>Hello world!</h1>"); printf("</body></html>"); return 0; }
>gcc hello.c -o hello.exe
カレントディレクトリから見てhtbin\hello.exeかcgi-bin\hello.exeのようにCGIを置く。
先ほどのコマンドでCGIHTTPServerを起動して、ブラウザで見てみる。
動いた。便利。
*1:Cはあんまり書いたことないので、突っ込みたのんます。