Home >Backend Development >Python Tutorial >Django basic tutorial summary
For all web applications, they are essentially a socket server, and the user's browser is actually a socket client.
A simple web program:
#!/usr/bin/env python #coding:utf-8 import socket def handle_request(client): buf = client.recv(1024) client.send("HTTP/1.1 200 OK\r\n\r\n") client.send("Hello, Seven") 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()
Python web programs are generally divided into server programs and application programs. The server program is responsible for encapsulating the socket server and sorting out various requested data when the request comes. The application is responsible for specific logical processing. In order to facilitate the development of applications, many web frameworks have emerged. For example Django, Flask, web.py, etc.
Different frameworks have different development methods, but no matter what, the developed application must cooperate with the server program to provide services to users. In this way, the server program needs to provide different support for different frameworks. Such a chaotic situation is not good for either the server or the framework. For servers, it is necessary to support various frameworks. For frameworks, only servers that support it can be used by developed 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, then they can be used together. Once the standards are determined, both parties implement them individually. In this way, the server can support more frameworks that support standards, and the framework can use more servers that support standards.
WSGI
web server gateway interface is a specification that defines the interface format between web app and web server written in python, realizing web app and Decoupling between web servers.
The independent wsgi server provided by the python standard library is called wsgiref
#!/usr/bin/env python #coding:utf-8 from wsgiref.simple_server import make_server def RunServer(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) return '<h1>Hello, web!</h1>' if __name__ == '__main__': httpd = make_server('', 8000, RunServer) print "Serving HTTP on port 8000..." httpd.serve_forever()
Custom web framework
Develop your own web framework through the wsgiref module provided by the python standard library
#!/usr/bin/env python #coding:utf-8 from wsgiref.simple_server import make_server def index(): return 'index' def login(): return 'login' def routers(): urlpatterns = ( ('/index/',index), ('/login/',login), ) return urlpatterns def RunServer(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) url = environ['PATH_INFO'] urlpatterns = routers() func = None for item in urlpatterns: if item[0] == url: func = item[1] break if func: return func() else: return '404 not found' if __name__ == '__main__': httpd = make_server('', 8000, RunServer) print "Serving HTTP on port 8000..." httpd.serve_forever()
The above is the detailed content of Django basic tutorial summary. For more information, please follow other related articles on the PHP Chinese website!