pmasiar
January 5th, 2007, 07:20 PM
Someone complained that Python web app tutorials are harder than PHP. So I created these two simple python apps to help you started.
I assume you have cgi-enabled directory.
UPDATE 1: Find your python first. In terminal, type 'which python'.
Then change first line of following scripts according where your python is: /usr/bin/python or /usr/local/bin/python or whatever you have.
(end update1)
Simplest one (3 lines): place it in CGI-enabled directory, make executable by web user (or anybody, or all):
#!/usr/bin/python
import cgi
cgi.test()
It will print your CGI environment variables and you can be sure all is OK. If not, error is in your web server settings. Also, make sure that apache can read/execute this script: ie. make ir readable/executable by all.
More complicated (35 lines including 13 lines of HTML template), but still simple:
#!/usr/bin/python
import cgi
import cgitb; cgitb.enable()
import os
##---CONSTANTS -------
BGCOLOR = '#f0f0f0'
##---TEMPLATES ----------------------------------------------------
HTML = """content-type: text/html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>%(TITLE)s</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head><body bgcolor="%(COLOR)s"><h1>%(TITLE)s</h1>
<h2>Hello %(NAME)s</h2>
<hr /><h2> Who are you?</h2>
<form action="%(SCRIPT_NAME)s" method="POST" enctype="multipart/form-data">
<input name="person_name" type="text" size="40"><br />
<input name="submit" type="submit" value="Try">
</form>
</body></html>"""
def main():
FORM = cgi.FieldStorage()
person = FORM.getfirst('person_name')
if not person:
person = '(Nobody yet)'
next_script = os.environ['SCRIPT_NAME'] # name of this script: it submits to self
print HTML % dict(TITLE='Hello Web!', NAME=person,
SCRIPT_NAME=next_script, COLOR=BGCOLOR)
# idiomatic start of the main() program
if __name__ == '__main__': main()
It will ask for name, and after hitting submit, prints it in the page and asks for more. If you have this running, you are on the way.
Good luck!
I assume you have cgi-enabled directory.
UPDATE 1: Find your python first. In terminal, type 'which python'.
Then change first line of following scripts according where your python is: /usr/bin/python or /usr/local/bin/python or whatever you have.
(end update1)
Simplest one (3 lines): place it in CGI-enabled directory, make executable by web user (or anybody, or all):
#!/usr/bin/python
import cgi
cgi.test()
It will print your CGI environment variables and you can be sure all is OK. If not, error is in your web server settings. Also, make sure that apache can read/execute this script: ie. make ir readable/executable by all.
More complicated (35 lines including 13 lines of HTML template), but still simple:
#!/usr/bin/python
import cgi
import cgitb; cgitb.enable()
import os
##---CONSTANTS -------
BGCOLOR = '#f0f0f0'
##---TEMPLATES ----------------------------------------------------
HTML = """content-type: text/html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>%(TITLE)s</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head><body bgcolor="%(COLOR)s"><h1>%(TITLE)s</h1>
<h2>Hello %(NAME)s</h2>
<hr /><h2> Who are you?</h2>
<form action="%(SCRIPT_NAME)s" method="POST" enctype="multipart/form-data">
<input name="person_name" type="text" size="40"><br />
<input name="submit" type="submit" value="Try">
</form>
</body></html>"""
def main():
FORM = cgi.FieldStorage()
person = FORM.getfirst('person_name')
if not person:
person = '(Nobody yet)'
next_script = os.environ['SCRIPT_NAME'] # name of this script: it submits to self
print HTML % dict(TITLE='Hello Web!', NAME=person,
SCRIPT_NAME=next_script, COLOR=BGCOLOR)
# idiomatic start of the main() program
if __name__ == '__main__': main()
It will ask for name, and after hitting submit, prints it in the page and asks for more. If you have this running, you are on the way.
Good luck!