CGI(Common Gateway Interface)とは、Webブラウザから来た要求に対して、
WWWサーバでプログラムを起動する仕組みです。
これによって、入力データによって変化するWebページや、ファイルやデータベースにデータを保存するような処理を実行できるようになります。
サーバ側で起動されるプログラムは、標準入力から入力可能で、
標準出力に出力可能なものなら、
どんな種類のプログラム言語を使用しても作成できます。
実際は、CGI作成用としてPerl、Ruby、Pythonなどのスクリプト言語が良く使われます。
CGIの処理の流れは、以下のようになっています。
CGIを試してみるためには、CGIが使用できる設定がなされたWWWサーバが起動されている必要があります。
通常は、Apacheなどを使用しますが、ここでは簡単に実験するためにPythonによってシンプルなWWWサーバを起動しておきます。
入力データの無い、文字列を表示するだけのシンプルなCGIを作成します。
#!/usr/bin/python
print "Content-Type: text/plain"
print
print "hello world."
#!/usr/bin/python
print "Content-Type: text/plain"
Content-Type
は送信されたデータの内容を表します。
text/plain
は、テキストを表します。
text/html
になります。
print
print "hello world"
現在の日時を得るCGIを、HTMLデータによって表示してみます。
#!/usr/bin/python
# -*- coding: utf-8 -*-
html = '''Content-Type: text/html
<html>
<head>
<title>
現在の日時
</title>
</head>
<body>
<h1>
現在の日時
</h1>
<p>
%s
</p>
</body>
</html>
'''
import time
now = time.strftime('%Y/%m/%d %H:%M:%S')
print html % now
# -*- coding: utf-8 -*-
html = '''Content-Type: text/html
'''
は改行を含んだ文字列を表します。
次に、'''
が出てくるまでが一つの文字列です。
import time
now = time.strftime('%Y/%m/%d %H:%M:%S')
print html % now
%s
の部分に文字列nowを当てはめて出力します。
3つのボタンのうち、どのボタンが押されたかを表示するCGIを作成してみましょう。
<html>
<head>
<title>ボタン</title>
</head>
<body>
<h1>ボタン</h1>
<form action="cgi-bin/test3.cgi" method="post">
<input type="submit" name="botton1" value="1" />
<input type="submit" name="botton2" value="2" />
<input type="submit" name="botton3" value="3" />
</form>
</body>
</html>
#!/usr/bin/python
# -*- coding: utf-8 -*-
html = '''Content-Type: text/html
<html>
<head>
<title>どのボタンが押されたか</title>
</head>
<body>
<h1>どのボタンが押されたか</h1>
<p>
押されたボタンは、%s
</p>
</form>
</body>
</html>
'''
import cgi
f = cgi.FieldStorage()
if f.getfirst('botton1'):
btn = '1'
elif f.getfirst('botton2'):
btn = '2'
elif f.getfirst('botton3'):
btn = '3'
print html % btn
<form action="cgi-bin/test3.cgi" method="post">
<input type="submit" name="botton1" value="1" />
<input type="submit" name="botton2" value="2" />
<input type="submit" name="botton3" value="3" />
</form>
import cgi
f = cgi.FieldStorage()
if f.getfirst('botton1'):
b = '1'
print html % b
テキストフィールドに入力された文字列を表示するCGIを作成してみましょう。
<html>
<head>
<title>テキストフィールド</title>
</head>
<body>
<h1>テキストフィールド</h1>
<form action="cgi-bin/test4.cgi" method="post">
<input type="text" name="text" />
<input type="submit" />
</form>
</body>
</html>
#!/usr/bin/python
# -*- coding: utf-8 -*-
html = '''Content-Type: text/html
<html>
<head>
<title>テキストフィールドから入力</title>
</head>
<body>
<h1>テキストフィールドから入力</h1>
<p>
入力された文字 %s
</p>
</body>
</html>
'''
import cgi
f = cgi.FieldStorage()
txt = f.getfirst('text', '')
print html % cgi.escape(txt)
<input type="text" name="text" />
<input type="submit" />
txt = f.getfirst('text', '')
print html % cgi.escape(txt)
ファイルをアップロードし、ブラウザに表示するCGIを作成してみます。
<html>
<head>
<title>選択されたファイルの内容を取得する</title>
</head>
<body>
<h1>ファイルを選択</h1>
<form action="cgi-bin/test5.cgi" method="post" enctype="multipart/form-data">
<input type="file" name="file1" />
<input type="submit" />
</form>
</body>
</html>
#!/usr/bin/python
# -*- coding: utf-8 -*-
html = '''Content-Type: text/html
<html>
<head>
<title>選択されたファイルの内容</title>
</head>
<body>
<h1>選択されたファイルの内容</h1>
<p>
選択されたファイルの内容
<pre>
%s
</pre>
</body>
</html>
'''
import cgi
form = cgi.FieldStorage()
data = ''
if form.has_key('file1'):
item = form['file1']
if item.file:
data = item.file.read()
print html % cgi.escape(data)
<form action="test14.cgi" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
if form.has_key('file'):
item = form['file']
if item.file:
data = item.file.read()