zoukankan      html  css  js  c++  java
  • 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()

  • 相关阅读:
    TensorFlow使用细节 NO1
    tensorflow的keras实现搭配dataset 之二
    tensorflow的keras实现搭配dataset 之一
    windows程序设计 Unicode和多字节之间转换
    windows程序设计 Unicode和多字节
    windows程序设计 显示一个窗口
    windows程序设计 获取系统文件路径
    windows程序设计 基础
    windows程序设计 加载位图图片
    windows程序设计 创建一个新的窗口
  • 原文地址:https://www.cnblogs.com/z-qinfeng/p/12298231.html
Copyright © 2011-2022 走看看