使用Python脚本编写ASP

要让IIS支持Python编写的脚本,前提是得在IIS上安装Python脚本引擎,这边建议安装ActivePython 2.6.2,它是一个不错的Python开发IDE,安装ActivePython时会自动在IIS安装Python脚本引擎。

下边是用Python编写的一个ASP页面:

<%@LANGUAGE="python" CODEPAGE="936"%>
<!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=gb2312" />
<title>Python测试-Mzwu.Com</title>
</head>

<body>
<%
from Rectangle import *

rectangle = Rectangle(50,100)
area = rectangle.Area()

Response.Write(area)
Response.Write("<br/>")
Response.Write(rectangle.ToString())
Response.Write("<br/>")
Response.Write(str(rectangle.__dict__))
%>
</body>
</html>


Rectangle.py:

#Python 2.6.2
class Rectangle:
    '''Class Rectangle.

    description.'''
    
    #constructor
    def __init__(self,width,height):
        self.Width = width
        self.Height = height

    #public methods
    def Area(self):
        return self.Width * self.Height

    def ToString(self):
        return 'Width:%s,Height:%s' % (self.Width,self.Height)

说明:Rectangle.py必须放在sys.path其中一个路径中,否则程序运行会出错,提示:ImportError: No module named Rectangle!

评论: 0 | 引用: 0 | 查看次数: 4010
发表评论
登录后再发表评论!