Django Framework Development-web Framework

Source: Internet
Author: User

First, the web framework

 Web Framework (Web framwork) is a development framework to support 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.

Second, Wsgir 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.

Wsgiref

From Wsgiref.simple_server import make_serverdef application (environ, start_response):    start_response (' OK ', [ (' Content-type ', ' text/html ')])    return [b ' 

  

Third, DIY a web framework

models.py

Import pymysql# Connection Database conn = Pymysql.connect (host= ' IP ', port=3306, user= ' root ', passwd= ' 123 ', db= ' blog ') #db: library name # create cursor cur = Conn.cursor () # sql= "# CREATE TABLE UserInfo (#         ID int primary key,#         name varchar (+), #         password varchar (32 #         ) ' # cur.execute (sql) # sql = "INSERT into userinfo values (%s,%s,%s)" # var = [(1, ' Mike ', 123), (2, ' Tom ', 123)]# Cur.executemany (SQL, var) sql = ("select * from UserInfo where name= ' Mike '") Cur.execute (SQL) # commit conn.commit () # Close Pointer Object Cur.close () # Close Connection Object Conn.close ()

  

Startup file manage.py

From wsgiref.simple_server import make_serverfrom URLs import urlpatterndef applications (environ, start_response):    # Current request path, path    = Environ.get ("path_info")    print (path)    start_response (' OK ', [' Content-type ', ' Text/html '), ("Charset", "UTF8")]    func = None for    item in Urlpattern:        if Path = = Item[0]:            func = item[1]< C8/>break    if func:        return [func (environ)]    else:        return [B "

  

urls.py

Urlpattern = (    ("/login", login),    ("/favicon.ico", fav),)

  

views.py

Import datetimeimport pymysqlfrom urllib.parse import parse_qsdef Login (environ): With open ("templates/login.html", "RB        As F:data = F.read () return datadef index (environ): With open ("templates/index.html", "RB") as F:    data = F.read () return datadef fav (environ): With open ("Templates/favicon.ico", "RB") as F:data = F.read () Return datadef Reg (environ): With open ("templates/reg.html", "RB") as F:data = F.read () return datadef Tim ER (environ): now = Datetime.datetime.now (). Strftime ("%y-%m-%d%x") return Now.encode ("UTF8") def auth (request): TR    y: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 ("Utf-8") pwd = Data.get (b "pwd") [0].decode ("Utf-8") # Connection Database conn = Pymysql.connect (host= ' 10.1.2.71 ', p ort=3306, user= ' root ', pAsswd= ' Testjfz ', db= ' blog ') # Create cursor cur = conn.cursor () sql= "SELECT * from UserInfo where name= '%s ' and password= ' %s ' "% (user,pwd) Cur.execute (SQL) if Cur.fetchone (): 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"

  

Login.html

<! DOCTYPE html>

  

Backend.html

<! DOCTYPE html>

  

  

Django Framework Development-web Framework

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.