Write an ASP in Python with a detailed explanation

Source: Internet
Author: User
Tags cgi web
First, the rehabilitation of the ASP

Think of ASP Many people will say "ASP language is very egg ache, can't object-oriented, function single, a lot of things can not achieve" and so on. The above is wrong, one of the ASP is not a language is Microsoft used to replace CGI web framework, but we have been distorted in VBS is the default language of ASP, the ASP and VBS are equated. The second ASP is not a single feature. This web provides 5 objects (request, response, server, session, Appliaction) This is what ASP is born with, except that these things are scripting-level things that ASP uses. ASP, with the help of Asp.dll dynamic link library, can theoretically try out all scripting languages including (VBScript, JSScript, ActionScript, Perl, Python), so ASP is a very rich and flexible web framework

second, why use Python to write ASP

Python has recently been in full swing, very fire, he occupies a significant position in all areas of the world, the web is naturally not without him. Echosong has used the Django, web.py, and so on Python's own web framework. Because the work requires echosong a large part of the time is written in ASP. and VBS ASP really let people write a kind of want to die feeling, a lot of functions with a variety of C or other language to write DLL stability difficult to consider, and Echosong is a full fan of Python, 08 began to contact Python has been as a hobby has not broken, but has not been used for work.

Three, start to merge the two small partners together

1, ASP installation: With the installation of IIS ASP becomes the default installed Web framework

2. Installation of Activepython:activepython is a dedicated Python programming and debugging tool introduced by ActiveState company.

ActivePython contains a full Python kernel that calls directly to the official Python open source kernel, as well as the IDE that Python programming needs to use, plus some Python windows extensions, as well as a full access to Windows The service of APIs. ActivePython is not open source like pure Python, but it can also be downloaded for free. (Note that the version can only download 2.5, at the beginning Echosong also not download the 2.7 version of the results of ruthless 500 reasons are not clear, not enough 2.5 version is enough)
3, command line running C:\Python25\Lib\site-packages\win32comext\axscript\client\pyscript.py;
4, complete the above two steps can be handwritten python ASP

Four, the simple demo
Connect database file conn.asp (connect MSSQL database with pymssql)

The code is as follows:


<%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 ()
%>

Here you can freely import the relevant modules of Python!!!

data.asp file calls conn.asp data connection Execute SQL statement loop displays the value of the field to the page

The code is as follows:


<% @LANGUAGE = "python" codepage= "65001"%>





Untitled Document


<%
Reslist = Gmssql. ExecQuery ("Select admin_id, Admin_userid from admin")
%>








<%for (Admin_id,admin_userid) in ResList:Response.write (U " ") Response.Write (U") ")%>
Admin number Manage Account
"+str (admin_id) +""+str (Admin_userid) +"





Five, the advantage of writing ASP with Python

1, High code reuse: You can write your own project module, the usual code written in the Python module, and then all the server can be used to adjust the import

2. Try Python's best features: Python's powerful Python library has a lot of out-of-the-box functionality, rather than a traditional ASP (VBS script) with many compiled line-language DLLs.

3, complete object-oriented: VBS is a process-oriented language, the characteristics of the object is very weak, many object-oriented ideas can not be used.

Vi. Stability and performance considerations
The ability to do stress testing at the same time, with various parameters stronger than VBS, especially in connection with the database using some Python excellent open source pool processing module, so that many database bottlenecks mitigated. (The data is not on this computer when writing a blog post)

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.