mod_python

来源:互联网 发布:大数据时代与个人隐私 编辑:程序博客网 时间:2024/05/22 03:48
【初级版】
#!/usr/bin/python                                               #py文件#coding:utf8from mod_python import apache,utilimport MySQLdbfp = file("/var/www/html/disp.html","r")#导入模板html = fp.read()#渲染htmlconn = 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.OKdef 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%sdef saveemp(uname):#增加cursor = conn.cursor()cursor.execute("insert into employee(name) values(%s)",(uname))return htmldef 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" onclick="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,utilimport MySQLdbact_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.OKdef 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%sdef add_employee(req):    fp=file('/var/www/html/n7/ff.html','r')    global html    html=fp.read()    return htmldef 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:utf8from mod_python import apache,utilimport MySQLdbfp = file("/var/www/html/disp.html","r")#导入模板html = fp.read()                        #渲染htmlconn = 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.OKdef 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%sdef saveemp():                             #增加    uname=requ.get('uname','nobody')            uage =requ.get('uage',0)    cursor.execute("insert into employee(name,age) values(%s,%s)",(uname,uage))    return htmldef delemp():                               #删除    uid = requ.get('id',0)    cursor.execute("delete from employee where (id=%s)",uid)#根据id删除    return html


原创粉丝点击