Rapid Python Programming: GUI Creation, Django Web Server, Game Programming, and Stock Analysis by William Gunnells

Rapid Python Programming: GUI Creation, Django Web Server, Game Programming, and Stock Analysis by William Gunnells

Author:William Gunnells
Language: eng
Format: azw3, epub, pdf
Published: 2017-06-28T07:00:00+00:00


12 CGI, WSGI Framework Development

12.1 CGI

CGI is the quick and dirty way to bring your applications to the web. The "Common Gateway Interface" (CGI) is an Internet standard that defines how web server software generates pages using a programming language or scripting languages. Perl was the most common scripting language used for this. However nearly any language can be used. Most languages have libraries that can use the CGI, but even a Unix bash script can be used to build pages. For instructions on how to setup Apache on Mac visit this url: http://www.cgi101.com/book/connect/mac.html. For windows or linux visit http://www.apache.org

Below is an example of a bash script:

#!/bin/bash

echo " Content-type: text/html\n"

echo "<html>"

echo "<p>Hello world"

echo "</html>"

Here is an example of a python script:

#!/usr/bin/python

#firstweb.py

print 'Content-type: text/html\n\n'

print "hello world"

The primary thing here is "Content-type" and "print" statement. In Apache Web server you can place your files in the 'cgi-bin' directory to execute. You can also rename your files with the '.pl' extension which is Perl's default extension for 'cgi-bin' applications. If you have access to apache ''httpd.conf', you need to add the extension to the # 'AddType' handler such as:

#AddType application/x-httpd-pl .py .pyc

The '.py' or '.pyc' will allow you to execute your python applications in '/cgi-bin' directory. Test to make sure you can execute your application using “chmod +ax script.py.” Below is an example of using a variable.

#secweb.py

print 'Content-type: text/html\n\n'

var="Hello world"

print """

<html><head>

<title>Second Web Page</title>

</head><body>

<h1> Messasge to you: %s</h1>

</body>

</html>

""" % var

Notice also that I place most everything within the quotes print """ """ % var. Here is an example of a simple form.

#form.py

print "Content-type:text/html\n\n"

print """

<html>

<form action="pform.py" method="get">

First Name: <input type="text" name="fname"> <br>

Last Name: <input type="text" name="lname">

<input type="submit" value="Submit">

</form>

"""

However you can leave the above form in a regular HTML file. Make sure you call the correct file in the action statement action="pform.py" or action="/cgi-bin/pform.py" . All of these examples assume you are in the 'cgi-bin' directory. To do the actual work, you would use another file to retrieve the stored values in a form.

#pform.py

import cgi,cgitb

form=cgi.FieldStorage()

#get data from fields

firstname=form.getvalue('fname')

lastname=form.getvalue('lname')

print "Content-type:text/html\n\n"

print """

<html>

<h1>Entries from form</h1>

<p>First name is: %s

<p>Last name is: %s

<hr>

</html>

""" % (firstname,lastname)

Notice the import cgi, cgitb and FieldStorage() function.

>>>help('cgi')

Help on module cgi:

NAME

cgi - Support module for CGI (Common Gateway Interface) scripts.

FILE

/usr/lib/python2.6/cgi.py

MODULE DOCS

http://docs.python.org/library/cgi

DESCRIPTION

This module defines a number of utilities for use by CGI scripts

written in Python.

CLASSES

UserDict.UserDict

FormContentDict

FormContent

SvFormContentDict

InterpFormContentDict

FieldStorag

...

Here is the cgitb

>>>help('cgitb')

Help on module cgitb:

NAME

cgitb - More comprehensive traceback formatting for Python scripts.

FILE

/usr/lib/python2.6/cgitb.py

MODULE DOCS

http://docs.python.org/library/cgitb

DESCRIPTION

To enable this module, do:

import cgitb; cgitb.enable()

at the top of your script. The optional arguments to enable() are:

display - if true, tracebacks are displayed in the web browser

logdir - if set, tracebacks are written to files in this directory

context - number of lines of source code to show for each stack frame

format - 'text' or 'html' controls the output format

By default, tracebacks are displayed but not saved, the context is 5 lines

and the output format is 'html' (for backwards compatibility with the

original use of this module)

Alternatively, if you have caught an exception and want cgitb to display it

for you, call cgitb.handler(). The optional argument to handler() is a

3-item tuple (etype, evalue, etb) just like the value of sys.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.