python-flask框架基础:传入字符、整形、浮点型、文件路径参数以及图片

在这里插入图片描述

实现源码

import os
import json
from datetime import datetime
from flask import Flask, render_template,request,make_response

app = Flask(__name__)
 
#float,path,string 
#uuid:只接收符合uuid的字符串,一般用于表的主键

# 传入整形参数
@app.route('/post/<int:post_id>')
def show_post(post_id):
    post_id=post_id+131
    return '显示计算结果:%d' % post_id
 
 # 传入文件路径参数
@app.route('/path/<path:subpath>')
def show_subpath(subpath):
    # show the subpath after /path/
    return 'Subpath %s' % subpath
 
 
 # 默认参数在python下的所有内容
@app.route('/python/')
def hello_python():
    return 'Hello Python'; 

# 传入文件字符参数[文本与整形转化]
@app.route('/<id>/')
def h(id):
    id = int(id)
    id = str(20+id) 
    return id

# #文本
# @app.route('/<text>')
# def index(text):
# 
    # return 'hello %s '% text

# 在网页上显示图片

IMG_PATH = "./"
 # http://127.0.0.1:8980/display/test.png
@app.route('/display/<string:filename>', methods=['GET'])
def display_img(filename):
    request_begin_time = datetime.today()
    print("request_begin_time", request_begin_time)
    if request.method == 'GET':
        if filename is None:
            pass
        else:
            image_data = open(IMG_PATH + filename, "rb").read()
            response = make_response(image_data)
            response.headers['Content-Type'] = 'image/jpg'
            return response
    else:
        pass


 
if __name__ == '__main__':
    app.run(host='127.0.0.1',debug=True,port=8980)  
    
    
    # https://www.cnblogs.com/feng0815/p/14488963.html
    # https://zhuanlan.zhihu.com/p/79847375
    # https://codeantenna.com/a/kf6D3NrIZt
    # https://blog.csdn.net/dcrmg/article/details/81987808
    # https://blog.csdn.net/dchzxl/article/details/123623831
    # https://blog.csdn.net/m0_51004308/article/details/118769627
    # https://github.com/letiantian/flask-tutorial/blob/master/demo/flask-demo-005/HelloWorld/client.py

实现结果:

在这里插入图片描述

参考

    # https://www.cnblogs.com/feng0815/p/14488963.html
    # https://zhuanlan.zhihu.com/p/79847375
    # https://codeantenna.com/a/kf6D3NrIZt
    # https://blog.csdn.net/dcrmg/article/details/81987808
    # https://blog.csdn.net/dchzxl/article/details/123623831
    # https://blog.csdn.net/m0_51004308/article/details/118769627
    # https://github.com/letiantian/flask-tutorial/blob/master/demo/flask-demo-005/HelloWorld/client.py
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

源代码杀手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值