Python's Web Framework:
Django
I. The nature of the Web Framework:
For all web applications, essentially a socket server, the User's browser is actually a socket client.
1 #!/usr/bin/env python2 #Coding:utf-83 4 #the infrastructure of the web framework, which almost all developers are extending based on this underlying result. 5 6 ImportSocket7 8 defhandle_request (client):9BUF = Client.recv (1024)TenClient.send ("http/1.1 ok\r\n\r\n") oneClient.send ("Hello, Seven.") a - defMain (): -Sock =Socket.socket (socket.af_inet, Socket. Sock_stream) theSock.bind (('localhost', 8000)) -Sock.listen (5) - - whileTrue: +connection, address =sock.accept () - handle_request (connection) + connection.close () a at if __name__=='__main__': -Main ()
TWO. implementation of the Web Framework:
1. Based on the WSGI specification:
WSGI (Web Server Gateway Interface) is a specification that aims to provide a common API standard between the Web server and the Web framework layer, reducing interoperability and creating a unified invocation Approach. It solves many existing frameworks, such as django, Flask, web.py, and so on, and the Django application is quite Extensive.
A simple reference server (wsgirefs) is available in the Python standard library: wsgiref.simple_server. Wsgiserver.
1 #!/usr/bin/env python2 #Coding:utf-83 4 fromWsgiref.simple_serverImportMake_server5 6 defrunserver (environ, start_response):7Start_response ('OK', [('Content-type','text/html')])8 return ''9 Ten if __name__=='__main__': onehttpd = Make_server ("', 8000, Runserver) a Print "serving HTTP on port 8000 ..." -Httpd.serve_forever ()
2. Custom socket, representative of TORNADO.
Three. Customize a Web Framework:
Python's Web Framework Django