#!/usr/bin/env python
# -*- coding: utf-8 -*- 
# predoslom riadku je deklaracia kodovania
import sys
import cgi
import os
import cgitb
cgitb.enable()

if 'REQUEST_METHOD' in os.environ:
    if os.environ['REQUEST_METHOD']=='GET':
        response="""<html>
<head>
    <title>
        Testovací skript
    </title>
</head>
<body>
<form method="POST" action="/cgi-bin/form.py">
<input name="input1" type="text">
<input type="submit">
</form>"""
    elif os.environ['REQUEST_METHOD']=='POST':
        response="""<html>
<head>
    <title>
        Testovací skript
    </title>
</head>
<body>
"""

        response+="""<h2>
    CGI parametre
</h2>
<p>
    <table>
"""

params=cgi.FieldStorage()

for key in params:
    response+="""
        <tr>
            <td>
                %s
            </td>
            <td>
                %s
            </td>
        </tr>
""" % (key,params[key].value)
response+="""
    </table>
</p>
</body>
"""
    
sys.stdout.write("Content-type: text/html; charset=UTF-8\r\n")
sys.stdout.write("Content-length: %d\r\n" % len(response))
sys.stdout.write("\r\n")
sys.stdout.write(response)
