Flask 中的Response

  • 1、return "helloword"
from flask import Flask
# 实例化Flask对象 app
app = Flask(__name__)
@app.route('/index')
# 视图函数
def index():
return 'helloword'
  • 2 render_template("html文件")
from flask import Flask
from flask import render_template @app.route('/home')
def home():
# 模板存放路径
# 创建 templates文件夹;右键文件夹 ---》Mark Directory as --->templates ---jinja2
return render_template('home.html')
# 启动服务
app.run()
  • 3、redirect("/home")

    • ResponseHeaders 中加入了一个 Location:http://url # 实现重定向
    • 302 http status
    • 4xx 错误 客户端
    • 5xx 错误 服务端
    from flask import Flask,render_template,redirect
    # 实例化Flask对象 app
    app = Flask(__name__) # app中的route装饰器 路由
    @app.route('/index')
    # 视图函数
    def index():
    return 'helloword'
    @app.route('/home')
    def home():
    # 模板存放路径
    return render_template('home.html')
    @app.route("/re")
    def re():
    # 重定向
    return redirect("/home")
    # 启动服务
    app.run()
    Flask 特殊返回值
  • 4、send_file('文件路径') 返回文件

    • 打开文件并返回文件内容;自动识别文件类型,并且在浏览器自动加上ResponseHeaders并且加入Content-Type:文件类型,是可以被客户端识别的文件类型;不能识别的文件类型 比如 .exe文件 会下载处理 - -浏览器会下载

      x-ms x:二进制 ; ms 微软

from flask import Flask, send_file
@app.route('/get_file')
def get_file():
# 返回helloword.py里的内容 Content-Type: text/plain; charset=utf-8
return send_file('helloword.py')
# 返回图片 Content-Type: image/jpeg
return send_file('1.jpg')
# 返回MP4 或 MP3 Content-Type: voide/mp4
return send_file('2.mp4')
# 下载 这个exe文件 Content-Type: application/x-msdownload
return send_file('3.exe')
# 启动服务
app.run()
  • 5、jsonify('字符串或数据类型') 返回标准格式的Json字符串

    • Content-Type:application/json

    • # 比如Flask更新了  1.1.1
      # return d # 暂时不建议使用,兼容性
      # 直接返回dict时 本质上在执行jsonify(d)
    • API接口

    • 序列化json字符串

    • 编写ResponseHeaders 加入Conent-Type:application/json

from flask import Flask, jsonify

# 实例化Flask对象 app
app = Flask(__name__)
@app.route('/get_json')
def get_json():
dic = {
"name": "anwen"
}
# Content-Type:application/json
return jsonify(d)
# 比如Flask更新了 1.1.1
# return dic # 暂时不建议使用,兼容性;直接返回dic时 本质上在执行jsonify(d)
# 启动服务
app.run()

Flask 中的request

# -*- coding: utf-8 -*-
# @Time : 2019/9/24 11:07
# @Author : AnWen
import os from flask import Flask, render_template, request, redirect app = Flask(__name__)
app.debug = True # 报错 405 Method Not Allowed
# methods=['GET', 'POST'] 添加路由的装饰器允许请求方式,覆盖
@app.route('/login', methods=['GET', 'POST'])
def login():
# request.method 获取请求方式
if request.method == 'GET':
# 在Django request.GET 取出 URL 中的参数
# 在Flask 获取URL 中的参数
# print(request.url) # 请求地址 http://127.0.0.1:9999/login?id=wen
# print(request.url_charset) # URL 编码方式 utf-8
# print(request.url_root) # 请求地址 完整请求地址 host http://127.0.0.1:9999/
# print(request.url_rule) # 请求路由地址 /login
# print(request.values.to_dict()) # 接收所有(GET,POST)请求中的数据,包含了 URL 和 FormData 中的数据 {'id': 'wen'}
# print(request.args.get("id")) # 获取URL中的数据 字符串 wen
return render_template('login.html')
if request.method == 'POST':
# request.form获取 FormData中的数据
# print(request.form) # ImmutableMultiDict([('username', '123')])
# print(request.form.get('username')) # 123
# print(request.form.to_dict()) # {'username': '123'} # request.files 所有文件类型全部放在这里 包括二进制 音频视频
# print(request.files.get("myfile")) # 文件对象<FileStorage: 'Flask day01.md' ('application/octet-stream')>
# my_file = request.files.get('myfile')
# # my_file.save('bf.jpg') # 当前目录下会保存一个bf.jpg
# new_file = os.path.join('img',my_file.filename)
# my_file.save(new_file) # 保存原文件到文件夹中 # 获取其他数据
# request.headers
# request.cookies
# request.path == request.url_rule
# request.host == "127.0.0.1:9527"
# request.host_url == "http://127.0.0.1:9527/" # 特殊提交方式数据获取
# Content-Type:application/json
# request.json 获取Content-Type:application/json时提交的数据
# Content-Type 无法被识别 或 不包含Form字眼
# request.data 获取 原始请求体中的数据 b"" return '200 ok'
# return redirect('/') @app.route('/')
def index():
return render_template('index.html') if __name__ == '__main__':
app.run("0.0.0.0", 9999)

Falsk中的Request、Response的更多相关文章

  1. python-django_rest_framework中的request/Response

    rest_framework中的request是被rest_framework再次封装过的,并在原request上添加了许多别的属性: (原Django中的request可用request._requ ...

  2. 使用 Masstransit中的 Request/Response 与 Courier 功能实现最终一致性

    简介 目前的.net 生态中,最终一致性组件的选择一直是一个问题.本地事务表(cap)需要在每个服务的数据库中插入消息表,而且做不了此类事务 比如:创建订单需要 余额满足+库存满足,库存和余额处于两个 ...

  3. 在Struts2的Action中获得request response session几种方法

    转载自~ 在Struts2中,从Action中取得request,session的对象进行应用是开发中的必需步骤,那么如何从Action中取得这些对象呢?Struts2为我们提供了四种方式.分别为se ...

  4. struts2的action中获得request response session 对象

    在struts2中有两种方式可以得到这些对象 1.非IoC方式 要获得上述对象,关键Struts 2中com.opensymphony.xwork2.ActionContext类.我们可以通过它的静态 ...

  5. 过滤器中的chain.doFilter(request,response)

    Servlet中的过滤器Filter是实现了javax.servlet.Filter接口的服务器端程序,主要的用途是过滤字符编码.做一些业务逻辑判断等.其工作原理是,只要你在web.xml文件配置好要 ...

  6. LoadRunner中取Request、Response

    LoadRunner中取Request.Response LoadRunner两个“内置变量”: 1.REQUEST,用于提取完整的请求头信息. 2.RESPONSE,用于提取完整的响应头信息. 响应 ...

  7. ASP.NET中的Request、Response、Server对象

    Request对象 Response.Write(Request.ApplicationPath) //应用根路径 Request.AppRelativeCurrentExecutionFilePat ...

  8. struts2中获取request、response,与android客户端进行交互(文件传递给客户端)

    用struts2作为服务器框架,与android客户端进行交互需要得到request.response对象. struts2中获取request.response有两种方法. 第一种:利用Servle ...

  9. JavaWeb(一)Servlet中的request与response

    一.HttpServletRequest概述 1.1.HttpServletRequest简介 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP ...

随机推荐

  1. 前端中的设计模式 JavaScript

    最近再准备秋招,然后顺便把过去空白的设计模式相关概念补一补,这些内容都是从<JavaScript设计模式与开发实践>一书中整理出来的 (1)单例模式 定义:保证一个类仅有一个实例,并提供一 ...

  2. 外观/门面模式(Facade)

    2015/4/28 外观/门面模式(Facade),为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. #include <vector> ...

  3. .NET Core ASP.NET Core Basic 1-2 控制反转与依赖注入

    .NET Core ASP.NET Core Basic 1-2 本节内容为控制反转与依赖注入 简介 控制反转IOC 这个内容事实上在我们的C#高级篇就已经有所讲解,控制反转是一种设计模式,你可以这样 ...

  4. Python Web Flask源码解读(二)——路由原理

    关于我 一个有思想的程序猿,终身学习实践者,目前在一个创业团队任team lead,技术栈涉及Android.Python.Java和Go,这个也是我们团队的主要技术栈. Github:https:/ ...

  5. Joda学习笔记

       Joda Time简介 日常业务开发中,经常需要处理日期.比如:获取当前一个月之内的开播记录,获取十分钟之内的红包记录等等.我们之前是用java.util.Calendar实现的,直到我看到占小 ...

  6. 关于web.xml配置

    整理自网上: web应用是一种可以通过Web访问的应用程序.在J2EE领域下,web应用就是遵守基于JAVA技术的一系列标准的应用程序. 最简单的web应用什么样? 2个文件夹.1个xml文件就能成为 ...

  7. SDU暑期集训排位(5)

    SDU暑期集训排位(5) A. You're in the Army Now 题意 类似选志愿.每个人有 mark,有优先级从高到低的志愿. 做法 定睛一看,鲨鼻题.然后 WA. 为什么会 WA 呢? ...

  8. HDU 1015 Safecracker (DFS)

    题意:给一个数字n(n<=12000000)和一个字符串s(s<=17),字符串的全是有大写字母组成,字母的大小按照字母表的顺序,比如(A=1,B=2,......Z=26),从该字符串中 ...

  9. eclipse中SpringBoot的maven项目出现无法解析父类的解决办法

    在eclipse中建立SpringBoot的maven项目时,继承父类,添加如下代码: <parent> <groupId>org.springframework.boot&l ...

  10. SpringBoot初体验之整合SpringMVC

    作为开发人员,大家都知道,SpringBoot是基于Spring4.0设计的,不仅继承了Spring框架原有的优秀特性,而且还通过简化配置来进一步简化了Spring应用的整个搭建和开发过程.另外Spr ...