ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: z_cgi.py
Submitter: Stephen Chappell (other recipes)
Last Updated: 2006/06/30
Version no: 1.0
Category: CGI Programming

 

Not Rated yet


Description:

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.

Source: Text Source

'''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()

Discussion:

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')



Add comment

No comments.



Highest rated recipes:

1. Implementation of sets ...

2. bag collection class

3. deque collection class

4. Floating Point Simulator

5. HTML colors to/from RGB ...

6. Select the nth smallest ...

7. Function Decorators by ...

8. MS SQL Server log monitor

9. Table objects with ...

10. wx twisted support using ...




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.