• wsgi返回页面优化


    # _author:来童星
    # date:2020/2/20
    # wsgi 框架
    from wsgiref.simple_server import make_server


    # environ为一个对象,封装了客户端的请求信息(environ是一个包含所有请求信息的dict对象)
    # start_response为服务器发送给浏览器(客户端)的响应信息

    def f1(request):
    return [b'<h1>hello star</h1>']
    def f2(request):
    return [b"<h1>hello light</h1>"]


    def application(environ, start_response):
    # print('environ',environ)#为一个字典
    # environ {'USERPROFILE': 'C:\Users', 'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',..}
    # print('PATH_INFO:',environ['PATH_INFO'])# 127.0.0.1 - - [20/Feb/2020 17:32:24] "GET /star/come/up HTTP/1.1" 200 18
    start_response("200 ok ", [('Content-Type', 'text/html')])
    path = environ['PATH_INFO']
    if path == "/star":
    return f1(environ)
    elif path == "/light":
    return f2(environ)
    else:
    return ["<h1>404</h1>".encode('utf8')]


    # wsgi作用:
    # 1.wsgi帮我们封装了socket对象以及准备过程(准备过程包括 socket对象的创建,bind,listen)
    # 2.通过environ将所有请求信息封装成一个对象
    # 3.通过start_response可以很方便的设置响应头
    httpd = make_server('', 8080, application)
    print('sereing HTTP is running on port 8080 ')

    # 开始监听HTTP请求
    httpd.serve_forever()
  • 相关阅读:
    小白安装使用Redis
    Mysql的Sql语句优化
    maximo入门----用户使用提要
    时不时刷刷BOSS 看看技术需求
    2019.7.10整理
    docker使用入门
    docker之windows安装&centOS安装
    HashTable学习
    Hashmap学习
    红黑树学习
  • 原文地址:https://www.cnblogs.com/startl/p/12336815.html
Copyright © 2020-2023  润新知