Python path [Chapter 2]: Web framework, python Chapter 2
Web framework nature
1. As we all know, all Web applications are essentially a socket server. Your browser is actually a socket Client.
#!/usr/bin/env python# -*- coding:utf-8 -*-#-Author-Lianimport socketdef handle_request(client): buf = client.recv(1024) client.send("HTTP/1.1 200 OK\r\n\r\n".encode("utf-8")) client.send("Hello, Seven".encode("utf-8"))def main(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('localhost', 8000)) sock.listen(5) while True: connection, address = sock.accept() handle_request(connection) connection.close()if __name__ == '__main__': main()
Run the above program and access http: // 127.0.0.1: 8000/in a browser to display the sent information.
The above uses socket to realize its essence. For python web programs in real development, it is generally divided into two parts: server programs and applications. The server program encapsulates the socket server and sorts the request data when the request arrives. The application is responsible for specific logic processing. To facilitate application development, many Web frameworks have emerged, such as Django, Flask, and web. py. Different frameworks have different development methods, but in any case, developed applications must work with server programs to provide services for users. In this way, the server program needs to provide different support for different frameworks. This chaotic situation is not good for servers or frameworks. For servers, different frameworks must be supported. For the framework, only the servers that support it can be used by applications. At this time, standardization becomes particularly important. We can set up a standard. As long as the server program supports this standard and the framework also supports this standard, they can work together. Once the standards are determined, both parties shall implement each other. In this way, servers can support more standard frameworks, and more standard servers can be used in the framework. WSGI (Web Server Gateway Interface) is a standard, it defines the Interface format between web apps and web servers written in python, and decouples web apps from web servers.
2. The independent WSGI server provided by the python standard library is called wsgiref
#!/usr/bin/env python# -*- coding:utf-8 -*-#-Author-Lianfrom wsgiref.simple_server import make_serverdef RunServer(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) return ['
Less code for web interaction
Custom Web Framework
1. Simple framework
Develop your own Web framework through the wsgiref module provided by the python standard library
#! /Usr/bin/env python #-*-coding: UTF-8-*-#-Author-Lianfrom wsgiref. simple_server import make_serverdef handel_index (): return ['
2. template engine
In the previous step, all login and index are returned to the user's browser with a simple string. In actual Web requests, a complex string that complies with HTML rules is generally returned, therefore, we generally write the HTML that will be returned to the user in the specified file and then return it. For example:
<!DOCTYPE html>Data.html
<!DOCTYPE html>Index.html
#! /Usr/bin/env python #-*-coding: UTF-8-*-#-Author-Lianfrom wsgiref. simple_server import make_serverdef handel_index (): f = open('index.html ', 'rb') data = f. read () return [data,] # return ['
Although the above Code can return HTML content to users for complex pages, there is still a problem: how to return dynamic content to users?
3. Return dynamic page data
- Customize a set of special syntaxes and replace them
- Use the open-source tool jinja2 and follow its specified syntax
#! /Usr/bin/env python #-*-coding: UTF-8-*-#-Author-Lianfrom wsgiref. simple_server import make_serverdef handel_index (): f = open('index.html ', 'rb') data = f. read () data = data. replace (B 'index', "Cheng Ronghua eats bones ". encode ("UTF-8") return [data,] # return ['
4. WEB Framework
MVC Model View Controller database Template file business processing MTV Model Template View database Template file business processing