获取访问的url和访问方式
from flask
import request
with app.test_request_context(
'/login',method=
'POST'):
print(request.path)
print(request.method)
获取请求参数
with app.test_request_context(
'/login?a=10&b=20',method=
'GET'):
print(request.args[
'a'])
print(request.args[
'b'])
print(request.args.get(
'a',
30))
'''
with app.test_request_context('/login?a=10&b=20',method='GET'):
print(dir(request))
request其他参数:
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_get_file_stream', '_get_stream_for_parsing', '_is_old_module', '_load_form_data', '_parse_content_type', 'accept_charsets', 'accept_encodings', 'accept_languages', 'accept_mimetypes', 'access_route', 'application', 'args', 'authorization', 'base_url', 'blueprint', 'cache_control', 'charset', 'close', 'content_encoding', 'content_length', 'content_md5', 'content_type', 'cookies', 'data', 'date', 'dict_storage_class', 'disable_data_descriptor', 'encoding_errors', 'endpoint', 'environ', 'files', 'form', 'form_data_parser_class', 'from_values', 'full_path', 'get_data', 'get_json', 'headers', 'host', 'host_url', 'if_match', 'if_modified_since', 'if_none_match', 'if_range', 'if_unmodified_since', 'input_stream', 'is_json', 'is_multiprocess', 'is_multithread', 'is_run_once', 'is_secure', 'is_xhr', 'json', 'list_storage_class', 'make_form_data_parser', 'max_content_length', 'max_form_memory_size', 'max_forwards', 'method', 'mimetype', 'mimetype_params', 'module', 'on_json_loading_failed', 'parameter_storage_class', 'path', 'pragma', 'query_string', 'range', 'referrer', 'remote_addr', 'remote_user', 'routing_exception', 'scheme', 'script_root', 'shallow', 'stream', 'trusted_hosts', 'url', 'url_charset', 'url_root', 'url_rule', 'user_agent', 'values', 'view_args', 'want_form_data_parsed']
'''
request.form来获取,
文件上传可以通过
f = request.files[
'the_file']
f.save(
'/var/www/uploads/uploaded_file.txt')
cookies获取
username = request.cookies.get(
'username')
如果想设置cookie需要注意 from flask import make_response
@app.route('/')
def index():
resp = make_response(render_template(...))
resp.set_cookie(
'username',
'the username')
return resp
其他参数大家可以通过dir(request)来查看属性,大家逐一实验一下,或者在以后的博文中获取详细信息
转载请注明原文地址: https://ju.6miu.com/read-15367.html