wsgi服務器

wsgi服務器DRP原則:Don't repeat yourself一、wsgi接口:全稱 Web Server Gateway Interface (web服務器網關接口)    請求:request    響應:response        #author: wylkjj        #date:2019/6/6        from wsgiref.simple_server import make_server        def application(environ,start_response):            #經過environ封裝成一個全部請求信息的對象            start_response('200 OK',[('Content-Type','text/html')])            return [b'<h1>Hellow,Web!</h1>']        #封裝socket對象以及準備過程(socket,bind,listen)        httpd = make_server('',8080,application)        print('Serving HTTP on port 8000...')        #開始監聽請求:        httpd.serve_forever()    頁面跳轉的流程        #author: wylkjj        #date:2019/6/8        from wsgiref.simple_server import make_server        def application(environ,start_response):            #ptint('environ',environ["PATH_INFO"])            start_response('200 OK', [('Content-Type', 'text/html')])            path=environ["PATH_INFO"]            if path=='/book':                return [b'<h1>hello book!</h1>']            elif path=='/web':                return [b'<h1>hello wed!</h1>']            else:                return ["<h1>404</h1>".encode("utf8")]            return [b'<h1>hello world!</h1>']        httpd=make_server('',8080,application)        print('Serving HTTP on port 8000...')        httpd.serve_forever()    最簡版:        #author: wylkjj        #date:2019/6/8        from wsgiref.simple_server import make_server        def f1(request):            return [b'<h1>hello book!</h1>']        def f2(request):            return [b'<h1>hello wed!</h1>']        def routers():            urlpatterns=(                ('/book',f1),                ('/web',f2),            )            return urlpatterns        def application(environ,start_response):            #ptint('environ',environ["PATH_INFO"])            start_response('200 OK', [('Content-Type', 'text/html')])            path=environ["PATH_INFO"]            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")]            # if path=='/book':            #     return f1(environ)            # elif path=='/web':            #     return f2(environ)            # else:            #    return ["<h1>404</h1>".encode("utf8")]            return [b'<h1>hello world!</h1>']        httpd=make_server('',8080,application)        print('Serving HTTP on port 8000...')        httpd.serve_forever()二、動態顯示時間:    .py:        #author: wylkjj        #date:2019/6/8        from wsgiref.simple_server import make_server        import time        def current_time(request):            # 模板            cur_time = time.ctime(time.time())            f = open("current_time.html", "rb")            data = f.read()            data = str(data, "utf8").replace("!cur_time!", str(cur_time))            return [data.encode("utf8")]        def routers():            urlpatterns=(                ("/current_time",current_time),            )            return urlpatterns        def application(environ,start_response):            start_response('200 OK', [('Content-Type', 'text/html')])            path=environ["PATH_INFO"]            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")]            return [b'<h1>hello world!</h1>']        httpd=make_server('',8080,application)        print('Serving HTTP on port 8000...')        httpd.serve_forever()    html:    <!DOCTYPE html>    <html lang="en">    <head>        <meta charset="UTF-8">        <title>Title</title>    </head>    <body>    <h1>current_time: !cur_time! </h1>    </body>    </html>
相關文章
相關標籤/搜索