[Tutor] Re: CGI implementation

Lee Harr missive at hotmail.com
Mon Oct 11 14:58:08 CEST 2004


>>         All of this means that to be able to test CGI scripts on your 
>>local
>>machine, you'll need to install a web server. Apache, which can be
>>found at http://httpd.apache.org/ , is a powerful, secure and Free web
>>server
>
>Or for a quick & easy webserver (Windows only):
>http://www.analogx.com/contents/download/network/sswww.htm
>


Or you could go all out with python and use twisted:
http://www.twistedmatrix.com/


# acgi.rpy
from twisted.web import static, twcgi

path_to_python = '/usr/local/bin/python'
path_to_cgi_bin = '/usr/home/lee/python/cgi/'

class PyScript(twcgi.FilteredScript):
    filter = path_to_python

resource = static.File(path_to_cgi_bin)
resource.processors = {'.py': PyScript}


#simple.py
import cgi
import cgitb; cgitb.enable()


def main():
    print 'Content-Type: text/html\n\n'
    print '<html><head></head><body>'

    form = cgi.FieldStorage()

    if not form:
        print '''<form action="">

            <p>foo: <input name="foo">
            </p>

            <p><input type="submit">
            </p>
            </form>'''

    else:
        print '''
        <p>Your form contained:
        </p>
        '''

        for field in form:
            print '<p>%s: %s' % (field, form.getfirst(field))


    print '</body></html>'



if __name__ == '__main__':
    main()


# now create and start the server
# (I am not exactly sure how you would do this in windows...)
$ mktap web -p 9999 --resource-script /usr/home/lee/python/cgi/acgi.rpy
$ twistd -f web.tap


# Then browse to http://localhost:9999/

_________________________________________________________________
Tired of spam? Get advanced junk mail protection with MSN 8. 
http://join.msn.com/?page=features/junkmail



More information about the Tutor mailing list