本项目是cgi编程,有addmessage.py文件用于建立数据库和添加数据
main.cgi文件用于显示所有主题
view.cgi文件用于显示单个主题的详细信息
edit.cgi用于编辑主题和回复别人的主题
save.cgi用于保存
addmessage.py
#! /usr/bin/python
#addmessage.py
import sqlite3
conn = sqlite3.connect('messages.db')
conn.text_factory = str
curs = conn.cursor()
reply_to = raw_input('Reply to:')
subject = raw_input('Subject:')
sender = raw_input('Sender:')
text = raw_input('Text:')
curs.execute("select count(*) from sqlite_master where type = 'table' and name = 'messages'")
res = curs.fetchone()
if res[0]==0 :
curs.execute("""
create table messages(
id integer primary key autoincrement,
subject text not null,
sender text not null,
reply_to int,
text text not null
);
""")
if reply_to:
query = """
insert into messages(reply_to,sender,subject,text)
values(%s,'%s','%s','%s')"""%(reply_to.decode('utf-8'),sender.decode('utf-8'),subject.decode('utf-8'),text.decode('utf-8'))
else:
query = """
insert into messages(sender,subject,text)
values('%s','%s','%s')"""%(sender.decode('utf-8'),subject.decode('utf-8'),text.decode('utf-8'))
curs.execute(query)
curs.execute('select * from messages')
res = curs.fetchall()
print 'row:',curs.rowcount
for line in res:
for f in line:
print f,
print '\n'
conn.commit()
main.cgi文件
#!/usr/bin/python
import cgi
import cgitb;cgitb.enable()
import sqlite3
conn = sqlite3.connect('messages.db')
conn.text_factory = str
curs = conn.cursor()
curs.execute('select * from messages')
names = [d[0] for d in curs.description]
rows = [dict(zip(names,row)) for row in curs.fetchall()]
print """Content-type:text/html;charset = "utf-8"\n
<html>
<head>
<title>The FooBar Bulletin Board</title>
</head>
<body>
<h1>The FooBar Bulletin Board</h1>
"""
toplevel = []
children = {}
for line in rows:
if line['reply_to'] is None:
toplevel.append(line)
else:
children.setdefault(line['reply_to'],[]).append(line)
def format(row):
subjectView = "%(subject)s"%row
print '<p><a href="view.cgi?id=%(id)i">%(subject)s</a></p>'%row
try: kids = children[row['id']]
except KeyError:pass
else:
print "<blockquto>"
for kid in kids:
format(kid)
print "<blockquto>"
print '<p>'
for row in toplevel:
format(row)
print """</p>
<hr/>
<p><a href = "editBoard.cgi">Post message</a></p>
</body>
</html>
"""
view.cgi文件
#!/usr/bin/python
print "Content-type:text/html;charset = 'utf-8'\n"
import cgi
import sys
import cgitb;cgitb.enable()
import sqlite3
conn = sqlite3.connect('messages.db')
conn.text_factory = str
curs = conn.cursor()
form = cgi.FieldStorage()
idd = form.getvalue('id')
try: idd = int(idd)
except:
print 'Invalid Id'
sys.exit()
print """
<html>
<head>
<title>View Board</title>
</head>
<h1>View Board</h1>
<body>
"""
curs.execute('select * from messages where id = %i'%idd)
names = [d[0] for d in curs.description]
rows = [dict(zip(names,row)) for row in curs.fetchall()]
if not rows:
print "Unknown messages ID"
sys.exit()
row = rows[0]
print """
<b>Subject:</b>%(subject)s<br/>
<b>Sender:</b>%(sender)s<br/>
<pre>%(text)s</pre>
<hr/>
<a href = "simple_main.cgi">Back to the main Board</a>
<a href = "editBoard.cgi?reply_to=%(id)i">Reply</a>
</p>
</body>
</html>
"""%row
edit.cgi文件
#!/usr/bin/python
print "Content-type:text/html;charset = 'utf-8'\n"
import cgi
import cgitb;cgitb.enable()
import sys
import sqlite3
conn = sqlite3.connect('messages.db')
conn.text_factory = str
curs = conn.cursor()
form = cgi.FieldStorage()
reply_to = form.getvalue('reply_to')
print """
<html>
<head>
<title>Compose Board</title>
</head>
<body>
<h1>Compose Board</h1>
<form action = 'saveBoard.cgi' methon = 'POST'>
"""
subject = ''
if reply_to is not None:
print '<input type = hidden name = "reply_to" value = "%s" >'%reply_to
curs.execute('select subject from messages where id = "%s"'%reply_to)
subject = curs.fetchone()[0]
if not subject.startswith('Re: '):
subject = "Re: "+subject
print """
<b>Subject:</b><br/>
<input type = 'text' size = '40' name = 'subject' value = "%s"><br/>
<b>Sender:</b><br/>
<input type = 'text' size = '40' name = 'sender'><br/>
<b>Messages:</b><br/>
<textarea rows = '20' cols = '40' name = 'text'></textarea><br/>
<input type = 'submit' name = 'Save'>
</form>
<hr/>
<a href="simple_main.cgi">Back to main Board</a>
</body>
<html>"""%subject
save.cgi文件
#!/usr/bin/python
print 'Content-type:text/html;charset = "utf-8"\n'
import cgi
import cgitb;cgitb.enable()
import sys
import sqlite3
conn = sqlite3.connect('messages.db')
conn.text_factory = str
curs = conn.cursor()
form = cgi.FieldStorage()
reply_to = form.getvalue('reply_to')
sender = form.getvalue('sender')
subject = form.getvalue('subject')
text = form.getvalue('text')
if not(sender and subject and text):
print 'Please supply sender,subject and text'
sys.exit()
print """
<html>
<head>
<title>Save Board</title>
</head>
<body>
<h1>Save Board</h1>
"""
if reply_to :
curs.execute("insert into messages(reply_to,subject,sender,text) values(%i,'%s','%s','%s')"%(int(reply_to),subject,sender,text))
else:
curs.execute("insert into messages(subject,sender,text) values('%s','%s','%s')"%(subject,sender,text))
conn.commit()
print 'Save success!'
print """
<hr/>
<a href="simple_main.cgi">Back to main Board</a>
</body>
</html>
"""