request 上到底有哪些属性?url、base_url、url_root 有什么区别?哪些是 Flask 自己加的?请求体为什么只能读一次?这篇用一个真实请求把属性逐个打印出来。
request.accept_json = True
request.base_url = 'https://shop.example.test:8443/base/inspect/7'
request.content_length = 7
request.content_type = 'application/json'
request.endpoint = 'inspect_'
request.full_path = '/inspect/7?x=1&y=2'
request.host = 'shop.example.test:8443'
request.host_url = 'https://shop.example.test:8443/'
request.is_json = True
request.is_secure = True
request.method = 'POST'
request.mimetype = 'application/json'
request.path = '/inspect/7'
request.referrer = 'http://ref.test/'
request.remote_addr = '203.0.113.9'
request.scheme = 'https'
request.script_root = '/base'
request.type = 'flask.wrappers.Request'
request.url = 'https://shop.example.test:8443/base/inspect/7?x=1&y=2'
request.url_root = 'https://shop.example.test:8443/base/'
request.url_rule = '/inspect/<int:n>'
request.user_agent = 'lab/1.0'
request.view_args = {'n': 7}上一篇:URL 中的 Unicode 与编码;下一篇:Query String 参数解析。
flask.Request 自己定义的公开成员: ['blueprint', 'blueprints', 'endpoint', 'json_module', 'max_content_length', 'max_form_memory_size', 'max_form_parts', 'on_json_loading_failed', 'routing_exception', 'url_rule', 'view_args'] 继承链: ['flask.wrappers.Request', 'werkzeug.wrappers.request.Request', 'werkzeug.sansio.request.Request']
构造后缓存里已有: ['cookies', 'headers'] | 访问 args 与 form 之后: ['args', 'cookies', 'files', 'form', 'headers']
get_data() 两次: b'hello body' b'hello body' | 随后读 stream: b'' 先读 stream 再 get_data(): b'hello body' b''
REMOTE_ADDR=10.0.0.2、X-Forwarded-For=198.51.100.7 → request.remote_addr = 10.0.0.2
每次构造并读取:3.2 µs