想获取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', '')
网友评论