美文网首页
web.py获取请求头中的数据

web.py获取请求头中的数据

作者: 空山晚来秋 | 来源:发表于2022-03-04 17:24 被阅读0次

    想获取headers中的数据,但没能找到明细的说明。
    官方wiki地址:

    https://webpy.org/cookbook/ctx.zh-cn
    

    web.py中,web.ctx 还有一个方法是 values(),可以查看所有请求头中的数据

    # 获取方式
    print(web.ctx.values())
    
    # 返回数据
    dict_values(['200 OK', [], '', {'ACTUAL_SERVER_PROTOCOL': 'HTTP/1.1', 'PATH_INFO': '/api/hello', 'QUERY_STRING': '', 'REMOTE_ADDR': '127.0.0.1', 'REMOTE_PORT': '58553', 'REQUEST_METHOD': 'GET', 'REQUEST_URI': '/api/hello', 'SCRIPT_NAME': '', 'SERVER_NAME': 'localhost', 'SERVER_PROTOCOL': 'HTTP/1.1', 'SERVER_SOFTWARE': 'Cheroot/8.6.0 Server', 'wsgi.errors': <_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>, 'wsgi.input': <cheroot.server.KnownLengthRFile object at 0x7f8d4da85940>, 'wsgi.input_terminated': False, 'wsgi.multiprocess': False, 'wsgi.multithread': True, 'wsgi.run_once': False, 'wsgi.url_scheme': 'http', 'wsgi.version': (1, 0), 'SERVER_PORT': '8889', 'HTTP_ACCEPT': '*/*', 'HTTP_ACCEPT_ENCODING': 'gzip, deflate, br', 'HTTP_ACCEPT_LANGUAGE': 'zh-CN', 'HTTP_USER_AGENT': 'ApiPOST Runtime +https://www.apipost.cn', 'HTTP_CONNECTION': 'keep-alive', 'HTTP_TOKEN': 'eyJhbGciOiJIUzUxMiJ9-YZBjSaCHvqSkgQkY9tIVLQvTgA9C1FkS8Eg', 'HTTP_HOST': '127.0.0.1:8889', 'CONTENT_TYPE': 'application/x-www-form-urlencoded'}, {'ACTUAL_SERVER_PROTOCOL': 'HTTP/1.1', 'PATH_INFO': '/api/hello', 'QUERY_STRING': '', 'REMOTE_ADDR': '127.0.0.1', 'REMOTE_PORT': '58553', 'REQUEST_METHOD': 'GET', 'REQUEST_URI': '/api/hello', 'SCRIPT_NAME': '', 'SERVER_NAME': 'localhost', 'SERVER_PROTOCOL': 'HTTP/1.1', 'SERVER_SOFTWARE': 'Cheroot/8.6.0 Server', 'wsgi.errors': <_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>, 'wsgi.input': <cheroot.server.KnownLengthRFile object at 0x7f8d4da85940>, 'wsgi.input_terminated': False, 'wsgi.multiprocess': False, 'wsgi.multithread': True, 'wsgi.run_once': False, 'wsgi.url_scheme': 'http', 'wsgi.version': (1, 0), 'SERVER_PORT': '8889', 'HTTP_ACCEPT': '*/*', 'HTTP_ACCEPT_ENCODING': 'gzip, deflate, br', 'HTTP_ACCEPT_LANGUAGE': 'zh-CN', 'HTTP_USER_AGENT': 'ApiPOST Runtime +https://www.apipost.cn', 'HTTP_CONNECTION': 'keep-alive', 'HTTP_TOKEN': 'eyJhbGciOiJIUzUxMiJ9-YZBjSaCHvqSkgQkY9tIVLQvTgA9C1FkS8Eg', 'HTTP_HOST': '127.0.0.1:8889', 'CONTENT_TYPE': 'application/x-www-form-urlencoded'}, '127.0.0.1:8889', 'http', 'http://127.0.0.1:8889', '', 'http://127.0.0.1:8889', 'http://127.0.0.1:8889', '127.0.0.1', 'GET', '/api/hello', '', '/api/hello', [<web.application.application object at 0x7f8d487a2280>]])
    
    

    然后根据自己想要的key获取value就可以了

            token = web.ctx.env.get('HTTP_TOKEN', '')
    

    相关文章

      网友评论

          本文标题:web.py获取请求头中的数据

          本文链接:https://www.haomeiwen.com/subject/dxjmrrtx.html