Welcome, guest | Sign In | My Account | Store | Cart

z_cgi.py provides a simple interface for CGI applications. It does not currently support reading information from standard in. Future versions will probably be delivered in a packaged format.

Python, 79 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'''Support module for use by CGI scripts.

This module provides several functions and variables
that help with printing text and accessing form data.'''

__version__ = 1.3

################################################################################

class File:
    def __init__(*args, **kwargs):
        pass
    def flush(*args, **kwargs):
        pass
    def isatty(*args, **kwargs):
        pass
    def write(*args, **kwargs):
        pass
    def writelines(*args, **kwargs):
        pass

################################################################################

import sys

out = sys.stdout
sys.stdout = File()

def execute(main, exception):
    'Execute main unless exception.'
    if exception != string:
        main()
    else:
        print_self()

def print_html(text):
    'Print text as HTML.'
    out.write('Content-Type: text/html\n\n' + text)
    sys.exit(0)

def print_plain(text):
    'Print text as plain.'
    out.write('Content-Type: text/plain\n\n' + text)
    sys.exit(0)

def print_self():
    'Print __main__ as plain.'
    print_plain(file(sys.argv[0]).read())

################################################################################

import os

def export():
    'Exports string and dictionary.'
    global string, dictionary
    try:
        string = str(os.environ['QUERY_STRING'])
    except:
        string = str()
    try:
        dictionary = dict([(decode(parameter), decode(value)) for parameter, value in [item.split('=') for item in string.replace('+', ' ').split('&')]])
    except:
        dictionary = dict()

def decode(string):
    'Receives, decodes, and returns string.'
    index = string.find('%')
    while index != -1:
        string = string[:index] + chr(int(string[index+1:index+3], 16)) + string[index+3:]
        index = string.find('%', index + 1)
    return string

################################################################################

if __name__ == '__main__':
    print_self()
else:
    export()

If you have a CGI program that does not require the large CGI module found in the standard library, then you may want to consider this module instead. Like most code, it is always in developement and will probably support reading and processing information from Standard In somewhere in the near future. The following is program for testing the the z_cgi module is working correctly.

import z_cgi

def main():
    z_cgi.print_plain('string = ' + repr(z_cgi.string) + '\ndictionary = ' + repr(z_cgi.dictionary))

if __name__ == '__main__':
    z_cgi.execute(main, 'cgi')