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>
相关文章
相关标签/搜索