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. application/x-www-form-urlencoded ,multipart/form-data, text/plain

    APPLICATION/X-WWW-FORM-URLENCODED MULTIPART/FORM-DATA TEXT/PLAIN 后台返回的数据响应的格式类型 application/x-www-fo ...

  2. Swoole 实战:MySQL 查询器的实现(协程连接池版)

    目录 需求分析 使用示例 模块设计 UML 类图 入口 事务 连接池 连接 查询器的组装 总结 需求分析 本篇我们将通过 Swoole 实现一个自带连接池的 MySQL 查询器: 支持通过链式调用构造 ...

  3. Django 内置分页的简单使用

    1, 文档 https://docs.djangoproject.com/en/1.11.1/topics/pagination/ 2,视图 from django.core.paginator im ...

  4. 【小技巧】【App store切换为中文】

    为什么80%的码农都做不了架构师?>>>   贡献作者 -[XJDomain]博客XJ:  https://my.oschina.net/shengbingli/blogGitHub ...

  5. Linked List-3

    第一篇终结Linked List(一).终结Linked List(二)主要讲了单链表的基础知识,接下来的第二篇主要讲一些比较经典的问题. 一.Count() 给一个单链表和一个整数,返回这个整数在链 ...

  6. 网速慢?不!可能是DNS出了问题! 公共DNS优选之 BAT 百度、腾讯、阿里、谷歌DNS哪个更快?

    如果一下还是解决不了你的问题请这边走 首先是Google的DNS: 8.8.8.8 丢包严重 PASS但是扶墙的时候是必备的,如果有扶墙的需求的话可以备用. 二.百度DNS 180.76.76.76 ...

  7. 网络流--最大流--POJ 2139(超级源汇+拆点建图+二分+Floyd)

    Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the ...

  8. POJ - 2251 Dungeon Master(搜索)

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

  9. java学习first_day

    java枚举 public class EnumMethodDemo { enum Color {RED, GREEN, BLUE;} enum Size {BIG, MIDDLE, SMALL;} ...

  10. centos下的redis一键安装shell脚本

    #!/bin/bash yum install cpp binutils glibc-kernheaders glibc-common glibc-devel gcc make wget #安装依赖库 ...