python编程基础的读后感_《Python编程金典》读书笔记

ge forums

import os

import sys

from xml.dom.ext.reader import PyExpat

def printHeader( title, style ):

print """Content-type: text/html

&lt?xml version = "1.0" encoding = "utf-8"?&gt

&lt!DOCTYPE html PUBLIC

"-//W3C//DTD XHTML 1.0 Strict//EN"

"DTD/xhtml-strict.dtd"&gt

&lthtml xmlns = "http://www.w3.org/1999/xhtml"&gt

&lthead&gt

&lttitle&gt%s&lt/title&gt

&ltlink rel = "stylesheet" href = "%s" type = "text/css" /&gt

&lt/head&gt

&ltbody&gt""" % ( 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 """&lth1&gtmy Forum&lt/h1&gt

&ltp style = "font-weight:bold"&gtAvailable Forum&lt/p&gt

&ltul&gt"""

# 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 '&ltli&gt&lta href = "%s"&gt%s&lt/a&gt&lt/li&gt' % ( link, nameText )

print """&lt/ul&gt

&ltp style= "font-weight:bold"&gtForum Management&lt/p&gt

&ltul&gt

&ltli&gt&lta href = "addForum.py"&gtAdd a Forum&lt/a&gt&lt/li&gt

&ltli&gtDelete a Forum&lt/li&gt

&ltli&gtModify a Forum&lt/li&gt

&lt/ul&gt

&lt/body&gt

&lt/html&gt"""

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

&lt?xml version = "1.0" encoding = "utf-8"?&gt

&lt!DOCTYPE html PUBLIC

"-//W3C//DTD XHTML 1.0 Strict//EN"

"DTD/xhtml-strict.dtd"&gt

&lthtml xmlns = "http://www.w3.org/1999/xhtml"&gt

&lthead&gt

&lttitle&gt%s&lt/title&gt

&ltlink rel = "stylesheet" href = "%s" type = "text/css" /&gt

&lt/head&gt

&ltbody&gt""" % ( 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值