[Tutor] Python newbie
Roman Suzi
rnd@onego.ru
Sun, 1 Jul 2001 09:43:07 +0400 (MSD)
On Sat, 30 Jun 2001, TJ O'Grady wrote:
>Hi,
>
>I just finished my first python program. It does what
>I like, but I was wondering if there was a way to make
>it cleaner. At some point I'd like to make web pages
>totally customizeable based on initial user input, and
>the way I have it now would make for an unattractive
>script. Oh, and if anyone knows of a good place to
>find information on maintaining information from page
>to page, I would appreciate it the information.
You could use multiline """ ... """ to write HTML into:
#!/usr/bin/python
import cgi
form = cgi.SvFormContentDict()
fields=form.keys()
print """Content-type: text/html
<HTML>
<Head><Title>form handling at it's finest</Title></Head>
<body bgcolor="%(color)s">
<H1>Let's See What Happens</H1><hr>
<h2>Welcome, %(name)s</h2>
To our web page, and thanks for stopping by.
Come again Soon
""" % form
Plus probably you need to supply defaults in case
user has not supplied anything:
try:
print ...
except KeyError:
print """ who are you? what is your color?"""
ALso, frequently user input need to be checked for security. For example,
user could include malicious javascript or something like that and spoil
you page completely.
Probably replacing "<" and ">" into < > is good idea.
Sincerely yours, Roman Suzi
--
_/ Russia _/ Karelia _/ Petrozavodsk _/ rnd@onego.ru _/
_/ Sunday, July 01, 2001 _/ Powered by Linux RedHat 6.2 _/
_/ "DEVICE=EXXON.SYS may mess up your environment" _/