SoFunction
Updated on 2024-11-17

Summary of python web framework

1、Django

Django is probably the most iconic Python framework, an open source framework that follows the MMVC structural pattern. Its name comes from DjangoReinhardt, a French composer and guitarist, considered by many to be the greatest guitarist in history. The LawrenceJournal-World newspaper in the city of Lawrence, Kansas has two programmers, Adrian Holovaty and Simon Willison, who developed Django in 2003 to develop web programs for the newspaper.

2、TurboGears

TurboGears is a framework constructed by SQLAlchemy, WebOb, Repoze, Genshi, and other notable Python projects. In a sense, TurboGears is sticking together several already established open platforms. Like Django, it uses an MVC structure. Minimal patterns have also been included recently, which can be used as a microframework.

3、Flask

Flask is a python microframework based on Jinja2 and Werkzeug, similar to other frameworks. It is BSD-licensed with a small limited free software license. Websites that use Flask include Collage LinkedIN and Pinterest.

Knowledge Point Expansion:

socket-based

Handling requests yourself

#!/usr/bin/env python3
#coding:utf8
import socket
def handle_request(client):
 # Receive requests
 buf = (1024)
 print(buf)
 # Return information
 (bytes('<h1>welcome liuyao webserver</h1>','utf8'))
def main():
 # Create sock object
 sock = ()
 #Listening on port 80
 (('localhost',8000))
 # Maximum number of connections
 (5)
 print('welcome nginx')
 #Cycle
 while True:
 # Wait for the user to connect, the default accept blocking when there is a request down the execution of the
 connection,address = ()
 # Handle the connection to the handle_request function.
 handle_request(connection)
 #Close the connection
 ()
if __name__ == '__main__':
 main()

Based on wsgi

WSGI, Web Server Gateway Interface, or Python Web Server Gateway Interface, is a simple and generic interface between a Web server and a Web application or framework defined for the 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. as you can see from the name, this thing is a Gateway, that is, a gateway. The role of the gateway is to translate between protocols.

WSGI is designed as a low-level interface between a Web server and a Web application or application framework to enhance the common ground for portable Web application development.WSGI is based on existing CGI standards.

Many frameworks come with WSGI servers, such as Flask, webpy, Django, CherryPy, and so on. Of course, the performance is not good, the web server is more for testing purposes, the release is to use the production environment of the WSGI server or joint nginx do uwsgi.

The standalone 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 requests
 #Preprocess requests (encapsulates a lot of http request stuff)

To this summary of this article on the python web framework is introduced to this article, more related to the organization of python web framework content please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!