Web Framework
Web Frameworks (web framework) is a development framework that supports the development of dynamic Web sites, Web applications, and Web services. Most web frameworks provide a set of ways to develop and deploy a Web site, as well as a common set of methods for Web behavior. The web framework has implemented many features, and developers can quickly develop Web applications by using the approach provided by the framework and by completing their business logic. The browser and the server are communicating based on the HTTP protocol. It can be said that the web framework is in the above more than 10 lines of code base extension, there are many simple and convenient use of methods, greatly improving the efficiency of development.
wsgiref Module
The simplest Web application is to first save the HTML file, with a ready-made HTTP Server software, to receive user requests, from the file to read HTML, return.
If you want to generate HTML dynamically, you need to implement the above steps yourself. However, accepting HTTP requests, parsing HTTP requests, and sending HTTP responses are all menial jobs, and if we're writing these underlying code ourselves, it's going to take months to read the HTTP specification before we start writing Dynamic HTML.
The correct approach is that the underlying code is implemented by specialized server software, and we use Python to focus on generating HTML documents. Because we do not want to touch the TCP connection, HTTP RAW request and response format, we need a unified interface protocol to implement such Server software, let us concentrate on writing web business in Python. This interface is the Wsgi:web Server Gateway Interface. The Wsgiref module is a service module developed by Python based on the WSGI protocol.
From Wsgiref.simple_server import make_serverdef application (environ, start_response): start_response (' OK ', [ (' Content-type ', ' text/html ')]) return [b '
DIY a Web frameworkmodels.py
Import pymysql# Connection Database conn = Pymysql.connect (host= ' 127.0.0.1 ', port= 3306,user = ' root ', passwd= ', db= ' web ') #db: library name # Create cursor cur = conn.cursor () sql= "CREATE TABLE userinfo ( ID INT PRIMARY KEY, name varchar (+), password varchar (32)) " Cur.execute (SQL) #提交conn. Commit () #关闭指针对象cur. Close () #关闭连接对象conn. Close ()
Startup file manage.py
From wsgiref.simple_server import make_serverfrom app01.views import *import urlsdef routers (): urlpattern=urls. Urlpattern return urlpatterndef Applications (environ,start_response): path=environ.get ("Path_info") Start_response (' K OK ', [(' Content-type ', ' text/html '), (' Charset ', ' UTF8 ')]) urlpattern=routers () func= None for item in Urlpattern: if path==item[0]: func=item[1] break if func: return [func ( Environ)] else: return [B "
urls.py
From app01.views Import *urlpattern = ( ("/login/", login),)
Views
Import pymysqlfrom urllib.parse Import parse_qsdef Login (Request): If Request.get ("request_method") = = "POST": Try : request_body_size = Int (request.get (' Content_length ', 0)) except (ValueError): Request_body _size = 0 Request_body = request[' Wsgi.input '].read (request_body_size) data = Parse_qs (request_body) User=data.get (b "User") [0].decode ("UTF8") Pwd=data.get (b "pwd") [0].decode ("UTF8") #连接数据库 conn = Pymysql . Connect (host= ' 127.0.0.1 ', port= 3306,user = ' root ', passwd= ', db= ' web ') # DB: library name #创建游标 cur = conn.cursor () Sql= "SELECT * from userinfo WHERE NAME = '%s ' and PASSWORD = '%s '"% (user,pwd) Cur.execute (SQL) if cur.fetc Hone (): F=open ("templates/backend.html", "RB") Data=f.read () Data=data.decode ("UTF8") return Data.encode ("UTF8") else:print ("OK456") return B "user or PWD is wrong" El se:f = open ("temPlates/login.html "," RB ") data = F.read () f.close () return data
Login.html
<! DOCTYPE html>
Backend.html
<! DOCTYPE html>
Yuan the package is a Web framework that downloads this web framework to quickly implement some simple Web features, such as viewing time.
Python's web framework