Detailed description of asp written in python

Source: Internet
Author: User

I. ASP

Many people think of ASP as saying that "asp is a tough language, it cannot be object-oriented, it has a single function, and many things cannot be implemented. The above statements are all incorrect. ASp is not a language, but a web framework that Microsoft uses to replace CGI. However, we have been distorted in vbs, which is the default language of asp, equals the number between ASP and vbs. The second Asp function is actually not simple. This web provides five objects (request, response, server, session, appliaction). This is what asp is born, all of these are script-level items used by Asp. ASP uses Asp. dll dynamic link library. In theory, we can try all the scripting languages including vbscript, jsscript, actionscript, perl, and python. Therefore, ASP is a rich and flexible web framework.

Ii. Why use python to write Asp?

Python has been in full swing recently and is very popular. He plays an important role in all major fields, and naturally does not need it in the web field. Echosong has used python web frameworks such as django and web. py. Because of work requirements, Echosong is mostly written in ASP. However, the Asp of vbs makes people feel anxious to write, and it is difficult to consider the stability of many functions written in c or other languages, the echosong is a full fan of Python, and it has been a hobby that has never been interrupted since, but it has never been used to work.

3. integrate the two partners

1. asp installation: with IIS installation, asp becomes the web framework installed by default.

2. Install activepython: ActivePython is a dedicated Python programming and debugging tool released by ActiveState.

ActivePython contains a complete Python kernel, which directly calls the official Python open-source kernel. In addition, there is also the IDE required for Python programming, and some Python Windows extensions are attached, it also provides all services for accessing Windows APIs. Although ActivePython is not as open-source as pure Python, it can be downloaded for free. (Note that only version 2.5 can be downloaded. At the beginning, echosong cannot download version 2.7. The result of the relentless version 500 is unclear. The version 2.5 is enough)
3. Run C: \ Python25 \ Lib \ site-packages \ win32comext \ axscript \ client \ pyscript. py on the command line;
4. After completing the above two steps, you can write python Asp.

4. Simple Demo
Connect to the database file conn. asp (use pymssql to connect to the mssql database)

Copy codeThe 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.exe cute (SQL)
ResList = cur. fetchall ()
Self. conn. close ()
Return resList

Def ExecNonQuery (self, SQL ):
Cur = self. _ GetConnect ()
Cur.exe cute (SQL)
Self. conn. commit ()
Self. conn. close ()
Gmssql = MSSQL (host = "*****", user = "*****", pwd = "*****", db = "***")
Gcur = MSSQL. getCur ()
%>

Here we can freely import python related modules !!!

The data. asp file calls the conn. asp data connection to execute the SQL statement to display the field value cyclically to the page.

Copy codeThe Code is as follows:
<% @ 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> untitled document </title>
</Head>
<Body>
<%
ResList = gmssql. ExecQuery ("select admin_Id, admin_UserId from admin ")
%>
<Table>
<Tr> <td> administrator ID </td> <td> management account </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>


5. advantages of using python to write ASp

1. Highly code reuse: you can write your own project module, write common code into a python module, and then all the code on the server can be retrieved through import.

2. Try out the excellent features of python: Many ready-made functions of python's powerful Python library are used directly, instead of using the dll of many compilation lines in traditional asp (vbs script ).

3. completely object-oriented: vbs is a process-oriented language with weak object features, and many object-oriented ideas cannot be used.

 

Vi. Stability and performance considerations
I tested the ability to process transactions at the same time, and the parameters in various aspects were stronger than those in vbs. In particular, I used some excellent python open-source pool processing modules to connect to the database, reducing the bottleneck of many databases. (Related data is not on this computer when I write 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.