Summary of python web Framework

  • 2021-10-13 07:56:32
  • OfStack

1. Django

Django is probably the most representative Python framework and is an open source framework following the MMVC architectural pattern. His name comes from DjangoReinhardt, a French composer and guitarist, and many people think that he is the greatest guitarist in history. The LawrenceJournal-World newspaper in the city of Lawrence, Kansas, has two programmers, AdrianHolovaty and SimonWillison, who developed Django in 2003 to develop a web program for the newspaper.

2. TurboGears

TurboGears is the framework of famous Python projects such as SQLAlchemy, WebOb, Repoze and Genshi. In a sense, TurboGears is to glue multiple established open platforms into one. And Django1, adopting MVC structure. Recently, it also includes the minimum pattern, which can be used as a micro-framework.

3. Flask

Flask is an python microframework based on Jinja2 and Werkzeug, which is similar to other frameworks. It is a free software license authorized by BSD with a few restrictions. Websites using Flask include LinkedIn LinkedIN and Pinterest.

Extension of knowledge points:

Based on socket

Handle requests yourself


#!/usr/bin/env python3
#coding:utf8
import socket
def handle_request(client):
 # Receive a request 
 buf = client.recv(1024)
 print(buf)
 # Return information 
 client.send(bytes('<h1>welcome liuyao webserver</h1>','utf8'))
def main():
 # Create sock Object 
 sock = socket.socket()
 # Eavesdropping 80 Port 
 sock.bind(('localhost',8000))
 # Maximum number of connections 
 sock.listen(5)
 print('welcome nginx')
 # Cycle 
 while True:
 # Waiting for the user to connect , Default accept Blocking executes down when there is a request 
 connection,address = sock.accept()
 # Give the connection to handle_request Function 
 handle_request(connection)
 # Close the connection 
 connection.close()
if __name__ == '__main__':
 main()

Based on wsgi

WSGI, the full name of Web Server Gateway Interface, or Python Web Server Gateway Interface, is a simple and general interface between Web server and Web application program or framework defined for Python language. Since WSGI was developed, similar interfaces have appeared in many other languages.

The official definition of WSGI is the Python Web Server Gateway Interface. It can be seen from the name that this thing is an Gateway, that is, a gateway. The function of gateway is to convert between protocols.

WSGI is a low-level interface between Web server and Web application program or application framework, in order to enhance the common ground of portable Web application development. WSGI is designed based on the existing CGI standard.

Many frameworks come with WSGI server, such as Flask, webpy, Django, CherryPy and so on. Of course, the performance is not good. The self-contained web server is more for testing purposes. When it is released, it uses WSGI server in production environment or makes uwsgi in combination with nginx.

The stand-alone WSGI server provided by the python standard library is called wsgiref.


#!/usr/bin/env python
#coding:utf-8
# Import wsgi Module 
from wsgiref.simple_server import make_server

def RunServer(environ, start_response):
 start_response('200 OK', [('Content-Type', 'text/html')])
 return [bytes("welcome webserver".encode('utf8'))]

if __name__ == '__main__':
 httpd = make_server('', 8000, RunServer)
 print ("Serving HTTP on port 8000...")
 httpd.serve_forever()
 # Receive a request 
 # Preprocessing request ( Encapsulated a lot http Something requested )


Related articles: