mod_python

论坛 期权论坛 脚本     
已经匿名di用户   2022-4-16 00:32   1973   0
【初级版】
#!/usr/bin/python                                               #py文件
#coding:utf8
from mod_python import apache,util
import MySQLdb
fp = file("/var/www/html/disp.html","r")#导入模板
html = fp.read()      #渲染html
conn = MySQLdb.connect(host="localhost",user="root",passwd="",db="dragon")#创建游标

def handler(req):      #处理器

 requ = util.FieldStorage(req)
 req.content_type = "text/html"
 act = requ.get("act","disp")
 if act =="disp":     #显示
  html=disp() 
 elif act =="save":     #增加
  uname=requ.get('uname','nobody') 
  saveemp(uname)
  html=disp()
 elif act =="del":     #删除
  uid=requ.get('id',0) 
  delemp(uid)
  html = disp()
 req.write(html)
 return apache.OK

def disp():       #显示
 cursor = conn.cursor()
 cursor.execute("select * from employee")
 emps = cursor.fetchall()
 s=""
 for id,name in emps:
  s += "<div><span id='empid'>"+str(id) +"</span><span id='uname'>" + name + "</span><span><a href='?act=del&id="+str(id)+"'>del</a></span></div>"
 return html%s

def saveemp(uname):      #增加
 cursor = conn.cursor()
 cursor.execute("insert into employee(name) values(%s)",(uname))
 return html 

def delemp(uid):      #删除
 cursor = conn.cursor()
 cursor.execute("delete from employee where (id=%s)",uid)#根据id删除
 return html


<?xml version="1.0" encoding="UTF-8"?>                          <!--disp.html-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 <title></title>
</head>
<body>
<div id="emplist">%s</div>
<div><input type="button" value="add" οnclick="location.href='add.html?act=save'" /></div>
</body>
</html>

<?xml version="1.0" encoding="UTF-8"?>                          <!--add.html-->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 <title></title>
</head>
<body>
<form action="mptest.py">
name:<input type="text" name="uname" /><br />
<input type="hidden" name="act" value="save" />
<input type="submit" value="ok" />

</form>
</body>
</html>



字典版

#coding:utf8 
from mod_python import apache,util
import MySQLdb

act_dict={'add':'add_employee(req)',
          'disp':'disp_employee(req)',
          'save':'save_employee(req)',
          'del':'del_employee(req)'
         }

def handler(req):
    req.content_type='text/html'
    act= util.FieldStorage(req).get('act','disp')
    exec act_dict[act]
    req.write(html)
    return apache.OK

def disp_employee(req):
    fp=file('/var/www/html/n7/template.html','r')
    global html
    html=fp.read()
    conn=MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='DBemp')
    cursor=conn.cursor()
    cursor.execute('select * from employee')
    emps=cursor.fetchall()
    s=""
    i=1
    for id,name in emps:
        s+="<div><span class='empod'>"+str(i)+"</span><span class='ename'>"+name+"</span><span class='del'><a href='?act=del&id="+str(id)+"'>删除</a></sapn><div><br />"
        i+=1
    conn.close()
    html = html%s

def add_employee(req):
    fp=file('/var/www/html/n7/ff.html','r')
    global html
    html=fp.read()

    return html

def save_employee(req):
    global html
    uname=util.FieldStorage(req).get('uname','None')
    conn=MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='DBemp')
    cursor=conn.cursor()
    cursor.execute('''insert into employee (name) value('%s')'''%uname)
    conn.commit()
    cursor.close()
    conn.close()
    exec act_dict['disp']

def del_employee(req):
    global html
    uid=util.FieldStorage(req).get('id','None')
    conn=MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='DBemp')
    cursor=conn.cursor()
    cursor.execute('''delete from employee where id='%s' '''%uid)
    conn.commit()
    cursor.close()
    conn.close()
    exec act_dict['disp']


【高级简化版】
#!/usr/bin/python
#coding:utf8
from mod_python import apache,util
import MySQLdb
fp = file("/var/www/html/disp.html","r")#导入模板
html = fp.read()                        #渲染html
conn = MySQLdb.connect(host="localhost",user="root",passwd="",db="dragon")#创建游标
cursor = conn.cursor()

act_dict = { 
    'del':'html=delemp()',
    'disp':'html=dispemp()',
    'save':'html=saveemp()',
     }   
def handler(req):                       #处理器,control
    global requ
    req.content_type = "text/html"
    requ = util.FieldStorage(req)
    act = requ.get("act","disp")
    exec act_dict[act]                   #先执行动作
    exec act_dict['disp']                #立即刷新页面
    req.write(html)
    return apache.OK

def dispemp():                              #显示
    cursor.execute("select * from employee")
    emps = cursor.fetchall()
    s="<div id='love'><span>编号</span><span>姓名</span><span>年龄</span><span>删除</span></div>"
    for id,name,age in emps:
        s += "<div><span id='uid'>"+str(id) +"</span><span id='uname'>" + name + "</span><span id='uage'>"+str(age)+"</span><span><a href='?act=del&id="+str(id)+"'>del</a></span></div>"
    return html%s
def saveemp():                             #增加
    uname=requ.get('uname','nobody')        
    uage =requ.get('uage',0)
    cursor.execute("insert into employee(name,age) values(%s,%s)",(uname,uage))
    return html
def delemp():                               #删除
    uid = requ.get('id',0)
    cursor.execute("delete from employee where (id=%s)",uid)#根据id删除
    return html


分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP