Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6807721

Browse files
committed
Some really simple cgi examples. cgi3 is a MiniWiki.
1 parent 60a5d72 commit 6807721

4 files changed

Lines changed: 157 additions & 0 deletions

File tree

Demo/cgi/cgi0.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#! /bin/sh
2+
3+
# If you can't get this to work, your web server isn't set up right
4+
5+
echo Content-type: text/plain
6+
echo
7+
echo Hello world
8+
echo This is cgi0.sh

Demo/cgi/cgi1.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/local/bin/python
2+
3+
"""CGI test 1 - check server setup."""
4+
5+
# Until you get this to work, your web server isn't set up right or
6+
# your Python isn't set up right.
7+
8+
# If cgi0.sh works but cgi1.py, check the #! line and the file
9+
# permissions. The docs for the cgi.py module have debugging tips.
10+
11+
print "Content-type: text/html"
12+
print
13+
print "<h1>Hello world</h1>"
14+
print "<p>This is cgi1.py"

Demo/cgi/cgi2.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/local/bin/python
2+
3+
"""CGI test 2 - basic use of cgi module."""
4+
5+
import cgitb; cgitb.enable()
6+
7+
import cgi
8+
9+
def main():
10+
form = cgi.FieldStorage()
11+
print "Content-type: text/html"
12+
print
13+
if not form:
14+
print "<h1>No Form Keys</h1>"
15+
else:
16+
print "<h1>Form Keys</h1>"
17+
for key in form.keys():
18+
value = form[key].value
19+
print "<p>", cgi.escape(key), ":", cgi.escape(value)
20+
21+
if __name__ == "__main__":
22+
main()

Demo/cgi/cgi3.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#!/usr/local/bin/python
2+
3+
"""CGI test 3 (persistent data)."""
4+
5+
import cgitb; cgitb.enable()
6+
7+
import os, re, cgi, sys
8+
escape = cgi.escape
9+
10+
def main():
11+
form = cgi.FieldStorage()
12+
print "Content-type: text/html"
13+
print
14+
cmd = form.getvalue("cmd") or "view"
15+
page = form.getvalue("page") or "FrontPage"
16+
wiki = WikiPage(page)
17+
wiki.load()
18+
method = getattr(wiki, 'cmd_' + cmd, None) or wiki.cmd_view
19+
method(form)
20+
21+
class WikiPage:
22+
23+
homedir = os.path.dirname(sys.argv[0])
24+
scripturl = os.path.basename(sys.argv[0])
25+
26+
def __init__(self, name):
27+
self.name = name
28+
self.load()
29+
30+
def cmd_view(self, form):
31+
print "<h1>", escape(self.splitwikiword(self.name)), "</h1>"
32+
print "<p>"
33+
for line in self.data.splitlines():
34+
line = line.rstrip()
35+
if not line:
36+
print "<p>"
37+
continue
38+
words = re.split('(\W+)', line)
39+
for i in range(len(words)):
40+
word = words[i]
41+
if self.iswikiword(word):
42+
if os.path.isfile(self.mkfile(word)):
43+
word = self.mklink("view", word, word)
44+
else:
45+
word = self.mklink("new", word, word + "*")
46+
else:
47+
word = escape(word)
48+
words[i] = word
49+
print "".join(words)
50+
print "<hr>"
51+
print "<p>", self.mklink("edit", self.name, "Edit this page") + ","
52+
print self.mklink("view", "FrontPage", "go to front page") + "."
53+
54+
def cmd_edit(self, form, label="Change"):
55+
print "<h1>", label, self.name, "</h1>"
56+
print '<form method="POST" action="%s">' % self.scripturl
57+
s = '<textarea cols="70" rows="20" name="text">%s</textarea>'
58+
print s % self.data
59+
print '<input type="hidden" name="cmd" value="create">'
60+
print '<input type="hidden" name="page" value="%s">' % self.name
61+
print '<br>'
62+
print '<input type="submit" value="%s Page">' % label
63+
print "</form>"
64+
65+
def cmd_create(self, form):
66+
self.data = form.getvalue("text", "").strip()
67+
self.store()
68+
self.cmd_view(form)
69+
70+
def cmd_new(self, form):
71+
self.cmd_edit(form, label="Create Page")
72+
73+
def iswikiword(self, word):
74+
return re.match("[A-Z][a-z]+([A-Z][a-z]*)+", word)
75+
76+
def splitwikiword(self, word):
77+
chars = []
78+
for c in word:
79+
if chars and c.isupper():
80+
chars.append(' ')
81+
chars.append(c)
82+
return "".join(chars)
83+
84+
def mkfile(self, name=None):
85+
if name is None:
86+
name = self.name
87+
return os.path.join(self.homedir, name)
88+
89+
def mklink(self, cmd, page, text):
90+
link = self.scripturl + "?cmd=" + cmd + "&page=" + page
91+
return '<a href="%s">%s</a>' % (link, text)
92+
93+
def load(self):
94+
try:
95+
f = open(self.mkfile())
96+
data = f.read().strip()
97+
f.close()
98+
except IOError:
99+
data = ""
100+
self.data = data
101+
102+
def store(self):
103+
data = self.data
104+
try:
105+
f = open(self.mkfile(), "w")
106+
f.write(data)
107+
f.close()
108+
return ""
109+
except IOError, err:
110+
return "IOError: %s" % str(err)
111+
112+
if __name__ == "__main__":
113+
main()

0 commit comments

Comments
 (0)