Python 融于ASP框架
一、ASP 的平反
想到 ASP 很多人会说 “asp 语言很蛋疼,不能面向对象,功能单一,很多东西实现不了” 等等诸如此类。 以上说法都是错误的,其一 ASp 不是一种语言是 微软用来代替 CGI 的一种 web 框架,只不过我们一直被扭曲在 vbs 就是 asp 的默认语言,把 ASP 和 vbs 之间划了等号。 其二 Asp 功能其实并不单一 此 web 提供 5 个对象 (request、 response、 server、 session、 appliaction)这就是 asp 与生俱来的东西,除了这些东西都是 Asp 所用的脚本级的东西。 而 ASP 借助了 Asp.dll 动态链接库,理论上可以试用一切脚本语言包括(vbscript 、jsscript、 actionscript、 perl 、python),所以说 ASP 是非常丰富的灵活的 web 框架
二、为什么要用 python 写 Asp
python 最近如火如荼,非常之火,他在各大领域都占有自己举足轻重的地位,web 方面自然也少不了他。 Echosong 已经用过 django 、web.py 等等 python 自己的 web 框架。由于工作需要 Echosong 很大一部分时间是在写 ASP。 而 vbs 的 Asp 实在让人写得有种 想死感觉,很多功能借助各种 c 或者其他语言写的 dll 稳定性难以考量,而 echosong 又是一个 Python 的 十足迷、08 年开始接触 python 一直是做为一种爱好没断过,只是一直没用于工作。
三、开始把两小伙伴融合在一起
1、asp 的安装 : 随着 IIS 的安装 asp 就成为了默认安装好的 web 框架
2、安装 activepython: ActivePython 是由 ActiveState 公司推出的专用的 Python 编程和调试工具。
四 、简单的 Demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | < % import pymssql class MSSQL: def __init__( self ,host,user,pwd,db): self .host = host self .user = user self .pwd = pwd self .db = db def __GetConnect( self ): if not self .db: Response.write(NameError, "No connec Info" ) self .conn = pymssql.connect(host = self .host,user = self .user,password = self .pwd,database = self .db,charset = "utf8" ) cur = self .conn.cursor() if not cur: Response.write(NameError, "connect Err" ) else : return cur def getCur( self ): return self .__GetConnect() def ExecQuery( self ,sql): cur = self .__GetConnect() cur.execute(sql) resList = cur.fetchall() self .conn.close() return resList def ExecNonQuery( self ,sql): cur = self .__GetConnect() cur.execute(sql) self .conn.commit() self .conn.close() gmssql = MSSQL(host = "****" ,user = "****" ,pwd = "***" ,db = "***" ) gcur = MSSQL.getCur() % > |
这里 可以自由的 import python 的相关模块!!!
data.asp 文件调用 conn.asp 的数据连接执行 sql 语句 循环显示字段的值到页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | < % @LANGUAGE = "python" CODEPAGE = "65001" % > <! - - #include file="conn.asp"--> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > <html xmlns = "http://www.w3.org/1999/xhtml" > <head> <meta http - equiv = "Content-Type" content = "text/html; charset=utf-8" / > <title>无标题文档< / title> < / head> <body> < % resList = gmssql.ExecQuery( "select admin_Id, admin_UserId from admin" ) % > <table> <tr><td>管理员编号< / td><td>管理账号< / td>< / tr> < % for (admin_Id,admin_UserId) in resList: Response.write(u "<tr><td>" + str (admin_Id) + "</td>" ) Response.write(u "<td>" + str (admin_UserId) + "<td></tr>" ) % > < / table> < / body> < / html> |
注意第一行 <%@LANGUAGE="python" CODEPAGE="65001"%> 告诉 Asp.dll 用的是 python 脚本。因为 conn.asp 文件是被 data 应用了,所以不需要加这行,如果直接执行 conn.asp 会报错,因为默认是 vbs 脚本。
运行 data.asp 就能在页面上面显示 相应的数据展示效果
五、用 python 写 ASp 的优势
1、高度代码复用: 可以写自己项目的模块,把平时常用的代码 写成 python 的模块,然后服务器上所有的都可以借助 import 来调取
2、试用 python 优秀特征: python 强大的 Python 库 很多现成的功能直接用,而不要想传统 asp(vbs 脚本的)借助 很多 编译行语言的的 dll 来实现
3、完全的面向对象: vbs 是面向过程的语言,对象的特征很弱,很多面向对象的思想不能用。
六、稳定性 和性能的考虑
做了压力测试 同一时间处理事务的能力,各方面参数强于 vbs 的,特别是在连接数据库用了些 python 优秀开源的池处理模块,使得很多数据库的瓶颈减轻
以下为性能压力测试
< 模拟的用户数,两种情况公用一个 >
<python 脚本连接数据 >
<python 吞吐情况 >
跟 vbs 的 ADO 做些对比 ADO 图没有截取过来,但是从吞吐量,和事务处理速度都比 Vbs ADO 要强,有兴趣的同学可以自己测试看看!!!