pythonの勉強3

cgiで動かす

任意のファイルを検索して
そのソースをとりあえず表示する。
こんなんでいいのかな?
あとタブを変換しないといけないかも...。

#! /usr/bin/python
# -*- coding: utf-8 -*-

import cgi
import os
import re


print "Content-type:text/html\n\n";
print """
<html>
<head>
<title>テスト</title>
</head>
<body>
<h2>ファイルの中を表示します</h2>
"""
file_name = "test3.html"
flg = False

for (root, dirs, files) in os.walk(r"./"):
	for f in files:
		if f == file_name :
			flg = True
			break

if flg == True:
	fileObj = open(file_name, "r")
	fileText = fileObj.read()
	fileObj.close()
	fileText = cgi.escape(fileText)
	fileText = re.sub("\n", "<br />", fileText)
	print fileText
else:
	print "ファイルの指定が間違っています<br />"
	

print """
</body>
</html>
"""
#end.