guestbook.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cgi
import cgitb
cgitb.enable()
import codecs
import sys
sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
form = cgi.FieldStorage()
if "text" in form:
fbook = open('guestbook.txt', 'a')
text = form["text"].value
print(text, file=fbook)
fbook.close()
lines = open('guestbook.txt', 'r').read()
print('''Content-Type: text/html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<form action="/cgi-bin/guestbook.py" method="POST">
<textarea name="text"></textarea>
<br>
<input type="submit">
</form>
''', lines.replace('\n', '\n<br>'), '''
</body>
</html>
''')