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

在这里插入图片描述

实现源码

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

猜你喜欢

转载自blog.csdn.net/weixin_41194129/article/details/125606242