From 68077210c2588241fc5e4e16d83c083fa4103108 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Wed, 16 Oct 2002 21:01:27 +0000 Subject: Some really simple cgi examples. cgi3 is a MiniWiki. --- Demo/cgi/cgi0.sh | 8 ++++ Demo/cgi/cgi1.py | 14 +++++++ Demo/cgi/cgi2.py | 22 +++++++++++ Demo/cgi/cgi3.py | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 157 insertions(+) create mode 100755 Demo/cgi/cgi0.sh create mode 100755 Demo/cgi/cgi1.py create mode 100755 Demo/cgi/cgi2.py create mode 100755 Demo/cgi/cgi3.py diff --git a/Demo/cgi/cgi0.sh b/Demo/cgi/cgi0.sh new file mode 100755 index 0000000..5cefcd3 --- /dev/null +++ b/Demo/cgi/cgi0.sh @@ -0,0 +1,8 @@ +#! /bin/sh + +# If you can't get this to work, your web server isn't set up right + +echo Content-type: text/plain +echo +echo Hello world +echo This is cgi0.sh diff --git a/Demo/cgi/cgi1.py b/Demo/cgi/cgi1.py new file mode 100755 index 0000000..5dd7d2c --- /dev/null +++ b/Demo/cgi/cgi1.py @@ -0,0 +1,14 @@ +#!/usr/local/bin/python + +"""CGI test 1 - check server setup.""" + +# Until you get this to work, your web server isn't set up right or +# your Python isn't set up right. + +# If cgi0.sh works but cgi1.py, check the #! line and the file +# permissions. The docs for the cgi.py module have debugging tips. + +print "Content-type: text/html" +print +print "

Hello world

" +print "

This is cgi1.py" diff --git a/Demo/cgi/cgi2.py b/Demo/cgi/cgi2.py new file mode 100755 index 0000000..d956f65 --- /dev/null +++ b/Demo/cgi/cgi2.py @@ -0,0 +1,22 @@ +#!/usr/local/bin/python + +"""CGI test 2 - basic use of cgi module.""" + +import cgitb; cgitb.enable() + +import cgi + +def main(): + form = cgi.FieldStorage() + print "Content-type: text/html" + print + if not form: + print "

No Form Keys

" + else: + print "

Form Keys

" + for key in form.keys(): + value = form[key].value + print "

", cgi.escape(key), ":", cgi.escape(value) + +if __name__ == "__main__": + main() diff --git a/Demo/cgi/cgi3.py b/Demo/cgi/cgi3.py new file mode 100755 index 0000000..bdb2cb7 --- /dev/null +++ b/Demo/cgi/cgi3.py @@ -0,0 +1,113 @@ +#!/usr/local/bin/python + +"""CGI test 3 (persistent data).""" + +import cgitb; cgitb.enable() + +import os, re, cgi, sys +escape = cgi.escape + +def main(): + form = cgi.FieldStorage() + print "Content-type: text/html" + print + cmd = form.getvalue("cmd") or "view" + page = form.getvalue("page") or "FrontPage" + wiki = WikiPage(page) + wiki.load() + method = getattr(wiki, 'cmd_' + cmd, None) or wiki.cmd_view + method(form) + +class WikiPage: + + homedir = os.path.dirname(sys.argv[0]) + scripturl = os.path.basename(sys.argv[0]) + + def __init__(self, name): + self.name = name + self.load() + + def cmd_view(self, form): + print "

", escape(self.splitwikiword(self.name)), "

" + print "

" + for line in self.data.splitlines(): + line = line.rstrip() + if not line: + print "

" + continue + words = re.split('(\W+)', line) + for i in range(len(words)): + word = words[i] + if self.iswikiword(word): + if os.path.isfile(self.mkfile(word)): + word = self.mklink("view", word, word) + else: + word = self.mklink("new", word, word + "*") + else: + word = escape(word) + words[i] = word + print "".join(words) + print "


" + print "

", self.mklink("edit", self.name, "Edit this page") + "," + print self.mklink("view", "FrontPage", "go to front page") + "." + + def cmd_edit(self, form, label="Change"): + print "

", label, self.name, "

" + print '
' % self.scripturl + s = '' + print s % self.data + print '' + print '' % self.name + print '
' + print '' % label + print "
" + + def cmd_create(self, form): + self.data = form.getvalue("text", "").strip() + self.store() + self.cmd_view(form) + + def cmd_new(self, form): + self.cmd_edit(form, label="Create Page") + + def iswikiword(self, word): + return re.match("[A-Z][a-z]+([A-Z][a-z]*)+", word) + + def splitwikiword(self, word): + chars = [] + for c in word: + if chars and c.isupper(): + chars.append(' ') + chars.append(c) + return "".join(chars) + + def mkfile(self, name=None): + if name is None: + name = self.name + return os.path.join(self.homedir, name) + + def mklink(self, cmd, page, text): + link = self.scripturl + "?cmd=" + cmd + "&page=" + page + return '%s' % (link, text) + + def load(self): + try: + f = open(self.mkfile()) + data = f.read().strip() + f.close() + except IOError: + data = "" + self.data = data + + def store(self): + data = self.data + try: + f = open(self.mkfile(), "w") + f.write(data) + f.close() + return "" + except IOError, err: + return "IOError: %s" % str(err) + +if __name__ == "__main__": + main() -- cgit v0.12