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
- mod_python
- mod_python
- apache+mod_python mod_python的安装与配置
- mod_python官方手册翻译
- [Python]Apache with Mod_python
- mod_python安装配置笔记
- Apache + MySQL + Python + Mod_python
- ubuntu mod_python 安装
- mod_python中文文档
- mod_python中文文档
- mod_python 不要搞我!!!
- mod_python 的一些用法
- zsi + wsdl + mod_python
- apache mod_python 安装
- mod_python安装及问题解决
- apache+mysql+mod_python+Django
- 配置 apache2 & mod_python
- 编译mod_python for win
- web容器 web服务器 servlet/jsp容器 之间的区别和关系是什么?
- linux shell编程(一)
- live555学习1(转)
- Linux操作系统下串口编程入门教程
- 清理IBM Relational ClearQuest 异常退出后的dmp 文件
- mod_python
- linux shell编程(二)
- 使用百度定位服务实现 GPS+基站+WI-FI+IP混合定位功能
- 苦逼三流小公司程序员这半年找工作经历(2)面试题目 .
- PHP DATE( )函数与系统时间差8小时解决办法
- 使用Flex中Image组件图片无法呈现问题分析
- linux shell编程(三)
- perl基础技巧:避免脚本在windows中闪一下就关闭
- linux shell编程(四)