Home > Backend Development > Python Tutorial > Django basic tutorial summary

Django basic tutorial summary

WBOY
Release: 2017-06-28 12:00:24
Original
1286 people have browsed it

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()
Copy after login

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 &#39;<h1>Hello, web!</h1>&#39;
if __name__ == &#39;__main__&#39;:
    httpd = make_server(&#39;&#39;, 8000, RunServer)
    print "Serving HTTP on port 8000..."
    httpd.serve_forever()
Copy after login

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 &#39;index&#39;
def login():
    return &#39;login&#39;
def routers():
    
    urlpatterns = (
        (&#39;/index/&#39;,index),
        (&#39;/login/&#39;,login),
    )
    
    return urlpatterns
def RunServer(environ, start_response):
    start_response(&#39;200 OK&#39;, [(&#39;Content-Type&#39;, &#39;text/html&#39;)])
    url = environ[&#39;PATH_INFO&#39;]
    urlpatterns = routers()
    func = None
    for item in urlpatterns:
        if item[0] == url:
            func = item[1]
            break 
    if func:
        return func()
    else:
        return &#39;404 not found&#39;
    
if __name__ == &#39;__main__&#39;:
    httpd = make_server(&#39;&#39;, 8000, RunServer)
    print "Serving HTTP on port 8000..."
    httpd.serve_forever()
Copy after login


The above is the detailed content of Django basic tutorial summary. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template