• Flask学习笔记05之请求与响应


    1. 请求
    # request.method
    # request.args
    # request.form
    # request.values
    # request.cookies
    # request.headers
    # request.path
    # request.full_path
    # request.script_root
    # request.url
    # request.base_url
    # request.url_root
    # request.host_url
    # request.host
    # request.files
    # obj = request.files['the_file_name'] #上传文件
    # obj.save('/var/www/uploads/' + secure_filename(f.filename)) # 保存文件

    2. 响应

    响应体:
    return “asdf” #字符串
    return jsonify({'k1':'v1'})  # json字符串
    return render_template('html模板路径')
    return redirect('/index.html')

    定制响应头:

    # 通过make_response 对象来封装响应头和响应体
    obj = make_response("响应体内容") 
    obj.headers['xxxxxxx'] = '123'
    obj.set_cookie('key', 'value')
    return obj

    from  flask import Flask, render_template, request, redirect, session, views, make_response, jsonify
    
    app = Flask(import_name=__name__)
    app.secret_key = 'flask'
    
    
    # app.debug = True
    
    
    @app.route('/index')
    def index():
        print(dir(request))
        # 请求相关
        print(request.method)
        print(request.args)
        print(request.form)
        print(request.values)
        print(request.cookies)
        print(request.headers)
        print(request.path)
        print(request.full_path)
        print(request.script_root)
        print(request.url)
        print(request.base_url)
        print(request.url_root)
        print(request.host_url)
        print(request.host)
    
        # 响应
        dict = {'name': 'ok'}
        # response = make_response("123")
        response = make_response(jsonify(dict))
        response.set_cookie(key="name", value="admin")
    response.headers['age'] = 18
    return response

    if __name__ == '__main__':
    app.run()

  • 相关阅读:
    mysql 赋给用户权限 grant all privileges on
    ubuntu下aptget安装小型的lamp环境
    Linux系统进程管理
    SQLChapter1_Overview of SQL Server
    SQLChapter2Querying Data
    SQLexercise
    SQLChapter4Managing Databases and Table
    JavaUI添加事件(二)
    java ActionEventDemo
    JavaUI弹出对话框
  • 原文地址:https://www.cnblogs.com/z-qinfeng/p/12298231.html
Copyright © 2020-2023  润新知