Django base (a) -web frame

A simple web framework

WSGI:Web Server Gateway Interface

A simple web framework:

step1:

#!/usr/bin/env python
# -*- coding: utf-8 -*- 
from wsgiref.simple_server import make_server

def application(environ,start_response):
    start_response("200 OK",[("Content-Type","text/html")])
    return [b'<h1>Hello,web!!</h1>']

httpd=make_server("",8080,application)
print("Serving HTTP on port 8080....")

#开始监听http请求
httpd.serve_forever()

note:

The entire application () function itself is not involved in any part of the parsing HTTP, that is, the underlying code does not need to write our own, we are only responsible to consider how to respond to the request can be at a higher level of. 

application () function must be called by WSGI server. There are many WSGI server complies with the specification, we can pick one to use. 

A built-in Python WSGI server, this module is called the wsgiref     
      
file application () function is an HTTP handler WSGI-compatible, it receives two parameters: 
        // Environ: a dict objects contain information for all HTTP requests; 
        // The start_response: a transmits the HTTP response function. 

In the file application () function call start_response ( '200 OK', [ ( 'Content-Type', 'text / html')]) to the HTTP response sent Header, Header attention only sent once, i.e. only called once start_response () function. The start_response () function takes two parameters, a HTTP response code, a HTTP Header List is a group represented by each 
one comprising two str Header with a tuple of FIG. 

Under normal circumstances, should the Content-Type headers to the browser. Many other common HTTP Header should be sent. 

Then, the function returns the value of b '<h1> Hello, web ! </ H1>' will be sent to the browser as Body HTTP response. 

With WSGI, we are concerned about is how to get the HTTP request this information from the environ dict object, and then construct HTML,
() Header transmitted by start_response, and finally returns Body.

step2:

print(environ['PATH_INFO'])
    path=environ['PATH_INFO']
    start_response('200 OK', [('Content-Type', 'text/html')])
    f1=open("index1.html","rb")
    data1=f1.read()
    f2=open("index2.html","rb")
    data2=f2.read()

    if path=="/yuan":
        return [data1]
    elif path=="/alex":
        return [data2]
    else:
        return ["<h1>404</h1>".encode('utf8')]

step3:

from wsgiref.simple_server import make_server

def f1():
    f1=open("index1.html","rb")
    data1=f1.read()
    return [data1]

def f2():
    f2=open("index2.html","rb")
    data2=f2.read()
    return [data2]

def application(environ, start_response):

    print(environ['PATH_INFO'])
    path=environ['PATH_INFO']
    start_response('200 OK', [('Content-Type', 'text/html')])


    if path=="/yuan":
        return f1()

    elif path=="/alex":
        return f2()

    else:
        return ["<h1>404</h1>".encode("utf8")]


httpd = make_server('', 8502, application)

Print ( 'the Serving HTTP Port ON 8084 ...') 

# start listening for HTTP requests: 
httpd.serve_forever ()

step4:

from wsgiref.simple_server import make_server


def f1(req):
    print(req)
    print(req["QUERY_STRING"])

    f1=open("index1.html","rb")
    data1=f1.read()
    return [data1]

def f2(req):

    f2=open("index2.html","rb")
    data2=f2.read()
    return [data2]

import time

def f3(req):        #模版以及数据库

    f3=open("index3.html","rb")
    data3=f3.read()
    times=time.strftime("%Y-%m-%d %X", time.localtime())
    data3=str(data3,"utf8").replace("!time!",str(times))


    return [data3.encode("utf8")]


def routers():
        ( '/ Yuan', F1),

    = the urlpatterns (
        ('/alex',f2),
        ("/cur_time",f3)
    )
    return urlpatterns


def application(environ, start_response):

    print(environ['PATH_INFO'])
    path=environ['PATH_INFO']
    start_response('200 OK', [('Content-Type', 'text/html')])


    urlpatterns = routers()
    func = None
    for item in urlpatterns:
        if item[0] == path:
            func = item[1]
            break
    if func:
        return func(environ)
    else:
        return ["<h1>404</h1>".encode("utf8")]

httpd = make_server('', 8518,the Application) 
# start listening for HTTP requests:

Print ( 'the Serving HTTP Port ON 8084 ...')


httpd.serve_forever()

Guess you like

Origin www.cnblogs.com/hujinzhong/p/11566926.html