使用PSP编写动态网站

使用PSP编写动态网站

 作者:ocean    撰写日期:2011-09-07~2011-09-18

博客链接:http://oceanspace.tk

        PSP?游戏机写网站?可能吗?不管你信不信,反正我不信,哈哈~此PSP非彼PSP的,其全称为Python Server Pages,这下明白了吧?是滴,就是用python做脚本写网页喽~学python的朋友可能就会问了?还有这东西?怎么没见有人用     啊?要怎么用啊?莫急,听我一一道来。

       什么样的人适合用PSP?

       简单地说,PSP适合于不懂PHP但懂Python的在校学生用来写动态网站课程设计。为什么PSP没有流行呢?听前辈们说是因为它相比其他写动态网站的语言来说没啥优势。如果你想做Python程序员,用Python来进行网站开发,那还是去学Django吧,PSP估计国内没有公司招人吧~但是对于像我这样的不想在网页制作学习上花太多时间但又要做网站课程设计的学生来说,PSP是个不错的选择,因为它上手实在太快了,只要你懂Python就行,就是在<%和%>之间写Python语句而已,你说简单不简单?

 

一、PSP环境配置

下面以我使用的ubuntu10.04下的配置为例:

1.安装apache:

sudo apt-get install apache2

 

2.安装mod-python:

sudo apt-get install libapache2-mod-python

 

3.sudo vi /etc/apache2/httpd.conf,添加:

<Directory /var/www>

        Options Indexes FollowSymLinks MultiViews

        AllowOverride All

        Order allow,deny

        allow from all

        AddHandler mod_python .py

        PythonHandler mod_python.publisher | .py

        AddHandler mod_python .psp .psp_

        PythonHandler mod_python.psp | .psp .psp_

        PythonDebug On

</Directory>

 

4.安装mysql和接口程序mysql-python

(1)安装mysql:

sudo apt-get install mysql-server

安装过程最后会让你输入mysql中root用户的密码。

 

(2)前续操作:

sudo apt-get install python-dev

sudo apt-get install libmysqld-dev

sudo apt-get install libmysqlclient-dev

 

(3)下载MySQL-python-1.2.3.tar.gz并解压(这里我解压到家目录)

下载地址:http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz/download

 

(4)开始安装mysql-python:

wget http://peak.telecommunity.com/dist/ez_setup.py (需要联网下载)

sudo python ez_setup.py

cd MySQL-python-1.2.3

编辑该目录下的setup_posix.py文件,将第26行mysql_config.path = "mysql_config"改为本机mysql_config的路径(可用whereis mysql_config获得),我这里是mysql_config.path = "/usr/bin/mysql_config"

sudo python setup.py build

sudo python setup.py install

安装完毕。

 

5.建立python-eggs目录并设置权限

cd /var/www

sudo mkdir .python-eggs

sudo chmod 777 .python-eggs/

 

6.重启apache:

sudo /etc/init.d/apache2 restart

 

说明:

(1)重启apache时可能遇到以下错误:

apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName

... waiting apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName

解决方法:

sudo vi /etc/apache2/httpd.conf

在文件中加入以下内容:

ServerName localhost

重启apache就不会报错了~

 

(2)MySQL需要进行一些字符集的设置才能正常显示中文:

alter database 数据库名 default character set 'utf8';

alter database test charset=utf8;

可以用show variables like 'character%';查看修改后的字符集设置

 

二、PSP基本用法

上面也说过了,PSP基本就是在<%和%>之间写Python语句,这里有一个介绍:http://www.modpython.org/live/current/doc-html/pyapi-psp.html

 

下面介绍一下基本用法:

写HTML元素

你可以直接在PSP脚本里写HTML的元素,但在满足一般条件时才生成某些HTML元素时就不太适合了,这时候可以用req.write("HTML语言")来写。

表单数据的获取

变量=form['表单数据名']

重定向

psp.redirect("URL连接")

处理单个和多个表单数据

这时可充分利用Python的异常处理机制,先试着把表单数据转换成整型,如果成功则是单个数据,如果类型异常则是多个数据

变量=form['表单数据名']

try:

    temp=int(变量)

…… #单个表单的处理

except TypeError:

    for data in form['orderno']:

        …… #多个表单数据的处理

数据库操作和中文支持

准确得说这属于python的内容,但还是有必要提一下。

cxn = MySQLdb.connect(user = '用户名',passwd = '密码')

cur = cxn.cursor()

cur.execute("set NAMES utf8")

cur.execute("USE test")

数据库操作就是cur.execute("MySQL语句")。在最后加这几句:

cur.close()

cxn.commit()

cxn.close()

其他没啥特殊的用法了,python怎么写就在<%和%>之间怎么写。

 

三、PSP应用示例

从数据库中读取新闻显示出来,由用户复选要删除的新闻,提交后从数据库中删除新闻。

 

从数据库中读取新闻显示的脚本:

<html>
<head>
<title></title>
</head>
<body>
<H1 align=center>删除新闻</h1>
<HR align="center" size="2" width="70%" color="#0000FF">
<H2>请选择您要删除的新闻(可多选),然后点击提交</H2>
<% import MySQLdb %>
<form method="POST" action="dealwithdelete.psp">
<%
cxn
= MySQLdb.connect(user = 'root',passwd = 'oceangg')
cur = cxn.cursor()
cur.execute(
"set NAMES utf8")
cur.execute(
"USE test")
cur.execute(
"SELECT num,title FROM news")
for data in cur.fetchall():
htmlstr
= "<p><input type='checkbox' name='news' value="+str(data[0])+">"+data[1]+"</p>"
req.write(htmlstr)
cur.close()
cxn.commit()
cxn.close()
%>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" value="提交" name="b1"></p>
</html>

处理删除的脚本:

<html>
<% import MySQLdb %>
<%
cxn
= MySQLdb.connect(user = 'root',passwd = 'oceangg')
cur = cxn.cursor()
cur.execute(
"set NAMES utf8")
cur.execute(
"USE test")

data
=form['news']
try:
temp
=int(data)
cur.execute(
"DELETE FROM news WHERE num=%d" % temp)
except TypeError:
for each in form['news']:
cur.execute("DELETE FROM news WHERE num=%d" % int(each))

cur.close()
cxn.commit()
cxn.close()
%>

<H3 align=center>删除成功!</H3>
<p align=center><A href=http://localhost/admin/operation.html>返回操作界面</A></p>
<p align=center><A href=http://localhost/news.psp>查看更新后的新闻</A></p>
</html>

    用户申请宿舍,填写完表单后做相应处理。首先从Checktu表中核查是否有相应学籍信息,没有则无法申请;再检查Register表,如果已申请宿舍则也无法再申请。可以申请的则根据房间类型查询符合要求的房间,再根据租赁时间计算出应付金额,并把房间号(四人间的有还有床号)显示给用户。

申请页面代码:

<html>
<head>
<title>宿舍申请</title>
</head>
<body>
<H1 align=center>宿舍申请</h1>
<HR align="center" size="2" width="70%" color="#0000FF">

<form method="POST" action="apply.psp">
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;填写申请前请确保您已经知道要选择的宿舍类型。如果您还不知道宿舍类型有哪些,请先阅读<A href = http://localhost/database/roomtype.html>宿舍类型信息</A></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请填写以下</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;姓名:<input type="text" name="name" size=20></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;学号:<input type="text" name="sno" size=20></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='radio' name='sex' value="男" checked>&nbsp;&nbsp;
<input type='radio' name='sex' value="女"></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;年龄:<input type="text" name="age" size=10></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;院系:<select name="department">
<option>计算机</option>
<option>物流</option>
<option>能动</option>
</select></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;手机:<input type="text" name="phone" size=20></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;申请宿舍类型:<select name="type">
<option>四人间</option>
<option>单人间</option>
</select></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;租用时间:<select name="time">
<option>一学期</option>
<option>一年</option>
</select></p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" value="提交" name="b1"></p>
</html>

处理申请的脚本:

<html>
<%
import MySQLdb
%>

<%
name
=form['name']
sno=form['sno']
sex=form['sex']
age=form['age']
department=form['department']
phone=form['phone']
roomtype=form['type']
howlong=form['time']

if cmp(howlong,"一学期") == 0:
time = 6
else:
time = 12
age
= int(age)

cxn
= MySQLdb.connect(user = 'root',passwd = 'oceangg')
cur = cxn.cursor()
cur.execute(
"set NAMES utf8")
cur.execute(
"USE test")
cur.execute(
"SELECT * FROM CheckStu WHERE Sno=%s AND Sname=%s" ,(sno,name))
temp
= cur.fetchone()
if temp is None:
req.write(
"<H3 align=center>申请失败!</H3>")
req.write(
"<p align=center>对不起,我们没有您的学籍信息,您无法申请宿舍,请核查您输入的个人信息是否正确</p>")
req.write(
"<p align=center><A href = http://localhost/database/index.html>返回首页</A></p>")
else:
cur.execute(
"SELECT * FROM Register WHERE Sno=%s LIMIT 0,1" % (sno))
templist
= cur.fetchone()
if templist is not None:
req.write(
"<H3 align=center>申请失败!</H3>")
req.write(
"<p align=center>对不起,您已经申请过宿舍了,不能重复申请</p>")
req.write(
"<p align=center><A href = http://localhost/database/index.html>返回首页</A></p>")
else:
cur.execute(
"INSERT INTO Student(Sno,Sname,Ssex,Sage,Sdept,Stel) VALUES('%s','%s','%s',%d,'%s','%s')" % (sno,name,sex,age,department,phone))
cur.execute(
"INSERT INTO Register(Rtime,Sno,Rtype) VALUES(NOW(),'%s','%s')" % (sno,roomtype))
cur.execute(
"SELECT Registerno FROM Register WHERE Sno=%s" % (sno))
templist
= cur.fetchone()
regisno
= templist[0]

if cmp(roomtype,"单人间") == 0:
cur.execute(
"SELECT Rno FROM private WHERE Registerno=0 AND Rsex='%s' LIMIT 0,1" % (sex))
templist
= cur.fetchone()
tRno
= templist[0]
cur.execute(
"UPDATE private SET Registerno=%s WHERE Rno=%s" % (regisno,tRno))
else:
cur.execute(
"SELECT Rno,Bedno FROM public WHERE Registerno=0 AND Rsex='%s' LIMIT 0,1" % (sex))
templist
= cur.fetchone()
tRno
= templist[0]
tBedno
= templist[1]
cur.execute(
"UPDATE public SET Registerno=%s WHERE Rno=%s AND Bedno=%s" % (regisno,tRno,tBedno))

cur.execute(
"SELECT Bname,Rnum,Ispublic FROM Room WHERE Rno=%s" % (tRno))
templist
= cur.fetchone()
Bname
= templist[0]
Rnum
= templist[1]
Ispublic
= templist[2]
if Ispublic == 0:
cur.execute(
"SELECT monthprice FROM private WHERE Rno=%s" % (tRno))
templist
= cur.fetchone()
monthprice
= templist[0]
total
= int(monthprice) * int(time)
else:
cur.execute(
"SELECT monthprice,Bedno FROM public WHERE Rno=%s" % (tRno))
templist
= cur.fetchone()
monthprice
= templist[0]
Bedno
= templist[1]
total
= int(monthprice) * int(time)
req.write(
"<H3 align=center>申请成功!</H3>")
if cmp(roomtype,"四人间") == 0:
req.write(
"<p align=center>您申请到的房间位于%s %s房间 %s床</p>" % (Bname,Rnum,Bedno))
else:
req.write(
"<p align=center>您申请到的房间位于%s %s房间 </p>" % (Bname,Rnum))
req.write(
"<p align=center>月租金为%s元,您租赁%s,因此您需要支付%s元</p>" % (monthprice,howlong,total))
req.write(
"<p align=center>请您尽快到后勤集团领取钥匙,如有疑问,请拨打123456咨询,祝您入住愉快!</p>")
req.write(
"<p align=center><A href = http://localhost/database/index.html>返回首页</A></p>")
cur.close()
cxn.commit()
cxn.close()
%>
</html>

四、总结

从上面的例子中很明显的看出PSP几乎就是写Python,所以如果你想写一个简单的动态网站,懂Python而不懂PHP,又没那么多时间学习Django的话,PSP绝对是个不错的选择。

参考文献:

http://www.modpython.org/live/current/doc-html/pyapi-psp.html

posted @ 2011-10-03 00:29  Happy博客创作团队  阅读(429)  评论(0编辑  收藏  举报