producing a web page in Python
What's the least I can do?
As usual in developing a computer program, it helps to get a
minimal program to work. Then add complications in the
smallest reasonable steps, checking that each step is successful.
Here are a series of such steps:
create a Python-generated static web page on your pc, served by
the PythonWebServer
These instructions assume you have already downloaded, installed,
and checked the PythonWebServer on your local pc, by following
the instructions at this link. In
addition these instructions assume you are "Running the Web Server"
as directed by the instructions, and that you remember (or can find)
the location of the PythonWebServer's cgi-bin folder.
- Use a code window in IDLE to create a minimal web page, by
copying and pasting the following lines
Or you can (please read all the instructions here): download
(right-click, then "Save As...") the file using the one of the
following links, depending upon which operating system you're
using: Windows or
Mac/Unix, and put the file
into your cgi-bin directory, which is a subdirectory of
the directory containing the PythonWebServer.py file and call it
ASimplePage.py If you're on a Mac or Unix system,
you'll have to change the file permissions of the file to
"executable" -- you can do this by bringing up a Terminal
window, navigating to the cgi-bin directory and using
the command "chmod +x *.py", like we've done in class.
-
#! /usr/bin/python
import cgitb;
cgitb.enable()
TheContentHeader =
'Content-type: text/html\n\n'
TheHTMLHeader =
'<html><head>'
TheBeginningOfBody =
'</head><body>'
TheHTMLFooter =
'</body></html>'
# ------------------------ The
main function ----------------
def main():
print
TheContentHeader + TheHTMLHeader
# Let's
assign a title
print
'<title>A Simple
Page</title>'
print
TheBeginningOfBody
#This is
where the real HTML page stuff should go.
print
'<h3><center>Yo!</center></h3>'
print
TheHTMLFooter
# Now, let's run the function,
to get it to print it all out...
main()
|
If you've pasted this into IDLE's code window, then save the
file into the PythonWebServer's cgi-bin
folder, with the filename ASimplePage.py
- Test the result by browsing to http://127.0.0.1:9000/cgi-bin/ASimplePage.py
or http://localhost:9000/cgi-bin/ASimplePage.py
- If that doesn't work, then go to the next section.
use Python to test your web page on your pc
- If you didn't do this in the section above, launch IDLE and
use File/Open to bring up the ASimplePage.py file in a
code window.
- Test the program using Run → Run Module. Expect to
see:
Content-type: text/html
<html><head>
<title>A Simple Page</title>
</head><body>
<h3><center>Yo!</center></h3>
</body></html>
|
- If you don't get this, try to fix it, and then re-run it
in IDLE...
- Then Test the result in a browser, by browsing to http://127.0.0.1:9000/cgi-bin/makeSmallStatic.py
run the Python program on a Stuy web server
- Use FileZilla (or another ftp client) to...
- ...copy ASimplePage.py to your public_html directory on a
Stuy web server (such as marge.stuy.edu).
- ...add "execute" to the "Public permissions" on to the file
you have just copied to the Stuy web server. Mr. Brooks
explains how to set permissions in his YouTube
video. In brief: right click on the
file → File Permissions → set "Public permissions"
to allow read and execute.
- Test the result in a browser, by browsing to the file on your
web (for example, marge.stuy.edu/~your.name/ASimplePage.py
- If / when the expected "Success!" fails to appear, use the Python
Checker to check for common errors in your program.
Demonstrate this checker with the test files
http://marge.stuy.edu/~peter.brooks/good.py
http://marge.stuy.edu/~peter.brooks/bad.py
complicate the Python program to include desired features
Here are some things to try, both in a program on your pc and after
copying it to a Stuy web server:
- Introduce an intentional error, so you will recognize the
format when you accidentally introduce an unintentional error.
For example, make the program evaluate the undefined
expression "1/0".
- Create another .py file with a different name.
For example, make count.py list the numbers
from 0 to 7 on a web page on your pc, then on a Stuy web server.