import os
from flask import Flask,render_template,redirect,jsonify,send_file
app=Flask(__name__) #开发中开启debug模式,也可以直接在app.run()中设置参数
# app.debug=True
# app.config['DEBUG']=True #(1)flask中的return类似django中的return HttpResponse()
#return直接返回文本内容,在1.1.1版本之后可以返回字符串、字典、元组等,
# The return type must be a string, dict, tuple, Response instance, or WSGI callable
@app.route('/')
def home():
return 'first_flask' # @app.route('/')
# def home():
# return {'key':'first_flask' } #(2)flask中的return render_template() 类似django中的return render()
#return render_teplate()返回静态文件页面
@app.route('/index')
def index():
return render_template('index.html') #(3)flask中的return redirect()类似django中的return redirect()重定向302临时
#return redirect()重定向请求
@app.route('/reback')
def reback():
return redirect('/index') #(4)flask中的jsonify()支持直接发送json数据类型,response-headers中的content-type:applicaiton/json
@app.route('/flask_json')
def flask_json():
return jsonify(['a',2]) #(5)flask中的return send_file()直接可以返回文件
#后端会对send_file返回的文件进行自动识别,类未识别或者浏览器不能解析的就会直接下载
@app.route('/flask_file')
def flask_file():
filepath=os.path.join(os.path.dirname(os.path.abspath(__file__)),'file')
filename='1.png' #Content-Type: image/png
#filename='1.mp3' #Content-Type: audio/mpeg
#filename='1.mp4' #Content-Type: video/mp4
# filename = '1.pdf' #Content-Type: application/pdf
# filename = '1.pptx' #Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation
# filename = '1.docx' #Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
# filename='1.zip' #Content-Type: application/x-zip-compressed
# filename='1.rar' #Content-Type: application/octet-stream
file=os.path.join(filepath,filename)
return send_file(file) if __name__ == '__main__':
#flak服务默认端口是5000,可以通过参数指定
# app.run()
app.run(host='192.168.16.14',port=8888,debug=True)

templates模板文件中的页面index.html:

  

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>登陆成功,欢迎来到index页面</h1>
<a href="">点击查看数据信息</a>
</body>
</html>

flask之response的更多相关文章

  1. python web开发-flask中response,cookies,session对象使用详解

    Response响应对象: 当一个web请求被服务器处理完后,会返回用户请求的响应,这时候就要用到响应对象,根据响应给用户的形式不同,响应对象有以下几种处理方式 如果返回的是一个合法的响应对象,它会从 ...

  2. 第二篇 Flask的Response三剑客及两个小儿子

    一.Response三剑客 (一)Flask中的HTTPResponse @app.route("/") #app中的route装饰器 def index(): #视图函数 ret ...

  3. 通过flask中的Response返回json数据

    使用flask的过程中,发现有时需要生成一个Response并返回.网上查了查,看了看源码,找到了两种办法: from flask import Response, json Response(jso ...

  4. flask中的response

    1.Response 在flask中你想向前端返回数据,必须是Response的对象,这里和django必须是HttpResponse 对象一样, 主要将返回数据的几种方式 视图函数中return 字 ...

  5. Flask初学者:视图函数/方法返回值(HTML模板/Response对象)

    返回HTML模板:使用“from flask import render_template”,在函数中传入相对于文件夹“templates”HTML模板路径名称字符串即可(默认模板路径),flask会 ...

  6. Flask中request与response参数

    目录 request response request from flask import Flask from flask import request app = Flask(__name__) ...

  7. flask返回自定义的Response

    from json import dumps from flask import Response from flask_api import status from protocol.errors_ ...

  8. web框架--flask

    flask介绍 Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请求并对请求 ...

  9. Inside Flask - flask.__init__.py 和核心组件

    Inside Flask - flask.__init__.py 和核心组件 简单的示例 首先看看一个简单的示例.使用 Flask ,通常是从 flask 模块导入 Flask . request 等 ...

随机推荐

  1. [Qt] QString 常用函数

    1. append(), prepend() 2. count(), size(), length() 这三个函数是相同的 3. trimmed() 去掉首尾空格 4. isNull() 对未赋值的字 ...

  2. Python3的日期和时间

    2019独角兽企业重金招聘Python工程师标准>>> python 中处理日期时间数据通常使用datetime和time库 因为这两个库中的一些功能有些重复,所以,首先我们来比较一 ...

  3. JavaScript面向对象那些东西-继承

    继承 父类里有些属性方法 子类想把父类中的这些属性方法 继承过来给子类自己的实例也用用 ( ps: →_→ 能不能专业点 没文化真可怕 ) 一.原型链继承 // 原型链继承:把子类的原型作为父类的实例 ...

  4. 谁需要GUI?快看Linux 终端生存之道

    完全在 Linux 终端中生存并不容易,但这绝对是可行的. 处理常见功能的最佳 Linux shell 应用 你是否曾想像过完完全全在 Linux 终端里生存?没有图形桌面,没有现代的 GUI 软件, ...

  5. 使用Codemirror打造Markdown编辑器

    前几天突然想给自己的在线编译器加一个Markdown编辑功能,于是花了两三天敲敲打打初步实现了这个功能. 一个Markdown编辑器需要有如下常用功能: 粗体 斜体 中划线 标题 链接 图片 引用 代 ...

  6. LTE无线网络优化简介

    LTE无线网络优化特点 覆盖和质量的估计参数不同 TD-LTE使用RSPP.RSRQ.SINR进行覆盖和质量的评估. 影响覆盖问题的因素不同 工作频段的不同,导致覆盖范围的差异显著:需要考虑天线模式对 ...

  7. Spring官网阅读(四)BeanDefinition(上)

    前面几篇文章已经学习了官网中的1.2,1.3,1.4三小结,主要是容器,Bean的实例化及Bean之间的依赖关系等.这篇文章,我们继续官网的学习,主要是BeanDefinition的相关知识,这是Sp ...

  8. 如何发挥Visual Studio 2019强大的编辑功能轻松编辑Keil项目

    本文地址:https://www.cnblogs.com/jqdy/p/12565161.html 习惯了VS的强大编辑功能,对Keil 5越来越深恶痛绝.查阅网络文章后按图索骥初步实现了VS编辑Ke ...

  9. 设计模式之GOF23外观模式

    外观模式 迪米特原则:一个软件实体应当尽可能少的与其他实体发生相互作用 外观模式核心:为子系统提供统一的入口,封装子系统的复杂性,便于客户端调用 相当于找了个代理帮你做了所有事而你只需要和代理打交道

  10. 【hdu1006】解方程

    http://acm.hdu.edu.cn/showproblem.php?pid=1006 这题坑了我好久,发现居然是一个除法变成了整除,TAT,所以建议在写较长的运算表达式的时候出现了除法尽量加个 ...