ge forums
import os
import sys
from xml.dom.ext.reader import PyExpat
def printHeader( title, style ):
print """Content-type: text/html
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN"
"DTD/xhtml-strict.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>%s</title>
<link rel = "stylesheet" href = "%s" type = "text/css" />
</head>
<body>""" % ( title, style )
# open XML document that contains the forum names and locations
try:
XMLFile = open( "../htdocs/XML/forums.xml" )
except IOError:
print "Location: /error.html\n"
sys.exit()
# parse XML document containing forum information
reader = PyExpat.Reader()
document = reader.fromStream( XMLFile )
XMLFile.close()
# write XHTML to browser
printHeader( "my Forum", "/XML/site.css" )
print """<h1>my Forum</h1>
<p style = "font-weight:bold">Available Forum</p>
<ul>"""
# determine client-browser type
if os.environ[ "HTTP_USER_AGENT"].find( "MSIE" ) != -1:
prefix = "../XML/"
else:
prefix = "forum.py?file="
# add links for each forum
for forum in document.getElementsByTagName( "forum" ):
#create link to forum
link = prefix + forum.attributes.item( 0 ).value
#get element nodes containing tag name "name"
name = forum.getElementsByTagName( "name" )[ 0 ]
#get Text node's value
nameText = name.childNodes[ 0 ].nodeValue
print '<li><a href = "%s">%s</a></li>' % ( link, nameText )
print """</ul>
<p style= "font-weight:bold">Forum Management</p>
<ul>
<li><a href = "addForum.py">Add a Forum</a></li>
<li>Delete a Forum</li>
<li>Modify a Forum</li>
</ul>
</body>
</html>"""
reader.releaseNode( document )
--- 论坛的默认页面 ---
===============================================================================
#!c:\python23\python.exe
# filename: addForum.py
# Adds a forum to the list
import re
import sys
import cgi
#4DOM packages
from xml.dom.ext.reader import PyExpat
from xml.dom.ext import PrettyPrint
def printHeader( title, style ):
print """Content-type: text/html
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN"
"DTD/xhtml-strict.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>%s</title>
<link rel = "stylesheet" href = "%s" type = "text/css" />
</head>
<body>""" % ( title, style )
form = cgi.FieldStorage()
# if user enters data in form fields
if form.has_key( "name" ) and form.has_key( "filename" ):
newFile = form[ "filename" ].value
#determine whether file has xml extension
if not re.match( "\w+\.xml$", newFile ):
print "Location: /error.html\n"
sys.exit()
else:
# create forum files from xml files
try:
newForumFile = open( "../htdocs/XML/" + newFile, "w" )
forumsFile = open( "../htdocs/XML/forums.xml", "r+" )
templateFile = open ( "../htdocs/XML/template.xml" )
except IOError:
print "Location: /error.html\n"
sys.exit()
# parse forums document
reader = PyExpat.Reader()
document = reader