zoukankan      html  css  js  c++  java
  • 六、请求与响应

    1、常用的请求信息

     1 @app.route("/home")
     2 def home():
     3     from flask import request
     4     # 常用请求信息
     5     print(request.method)
     6     print(request.args)
     7     print(request.form)
     8     print(request.files)
     9     print(request.cookies)
    10     print(request.headers)
    11     print(request.values)
    12     print(request.data)
    13     print(request.path)
    14     print(request.full_path)
    15     print(request.url)
    16     print(request.base_url)
    17     print(request.url_root)
    18     print(request.host)
    19     print(request.host_url)
    20     print(request.script_root)
    21     return "Hello, home!"

    2、常用的构造响应的方式(响应编辑Cookie,请求获取Cookie)

     1 @app.route("/")
     2 def index():
     3     # 响应的几种形式
     4     # 返回字符串
     5     # return "Hello"
     6     # 返回一个int,根据报错信息得知
     7     # string, dict, tuple, Response instance, or WSGI callable, but it was a int
     8     # The tuple must have the form (body, status, headers), (body, status), or (body, headers).
     9     # return (1 , 2, 3, 4)
    10     # 返回一个字典,必须是可序列化对象
    11     # return {"func": [1, 2, 3]}
    12     # 渲染模板
    13     # from flask import render_template
    14     # data = {}
    15     # return render_template("home.html", **data)
    16     # 重定向
    17     # from flask import redirect
    18     # return redirect("/home")
    19 
    20     # 构造一个响应
    21     response = make_response("Hello World")
    22     response.headers["token"] = "awdadjdkajdaadkjadjadja"
    23     response.set_cookie("k", "v")
    24     response.delete_cookie("session")
    25     return response
  • 相关阅读:
    一名中国联络官的来信
    中国女性出席1899年伦敦世界妇女大会
    曾在九江同文任教的中外人士若干
    金韵梅大夫略传
    为何高于四次的方程没有根式解?
    日军进攻九江的影像资料
    美以美会在九江
    九江同文中学与宝洁公司的甘布尔家族
    九江生命活水医院
    微信小程序获取用户信息签名解密C#
  • 原文地址:https://www.cnblogs.com/loveprogramme/p/13372361.html
Copyright © 2011-2022 走看看