• 基于Flask传输字符串/图像


    传输字符串

    服务端

    from flask import Flask,request
    
    app = Flask(__name__)
    
    @app.route("/delimgs",methods = ['GET', 'POST']) # 路由
    
    def _server():
        if request.method == "POST":
            delname = request.form.get('delname')
            return {''text': delname}
        else:
            return "<h1>please use post !</h1>"
    
    if __name__ == "__main__":
        host = '0.0.0.0'
        port = '8082'
        app.run(debug=True, host=host, port=port)
    

    客户端(请求)

    import requests
    
    s = requests
    data={'delname':'test.jpg'}
    r = s.post('http://0.0.0.0:8082/delimgs', data)
    
    print(r.text)
    

     传输图像

    服务端

    import cv2, base64
    from flask import Flask, request
    
    app = Flask(__name__)
    
    @app.route("/imgsimilar",methods = ['GET', 'POST'])
    
    def get_similar_img():
        if request.method == "POST":
            temp_img_base64 = request.form.get('imgbase64')
            temp_img_base64 = base64.b64decode(temp_img_base64)
            rand_img_name = ‘test.jpg’
            file = open(temp_img_path,'wb')
            file.write(temp_img_base64)
            file.close()
            return {'sign':1, 'text’:’schuss’}
        else:
            return "<h1>Get similar img, please use pust !</h1>"
    if __name__ == "__main__":
        host = '0.0.0.0'
        port = '8082'
        app.run(debug=True, host=host, port=port)

     客户端(请求)

    import requests
    import base64
    
    t1 = time.time()
    s = requests
    with open('2.jpg', 'rb') as f:
        imgbase64 = base64.b64encode(f.read())
    data={'imgbase64':imgbase64}
    r = s.post('http://0.0.0.0:8082/imgsimilar', data)
    
    print(r.text)
    
  • 相关阅读:
    896. 单调数列
    819. 最常见的单词
    collections.Counter()
    257. 二叉树的所有路径
    万里长征,始于足下——菜鸟程序员的学习总结(三)
    Ogre启动过程&原理
    Ogre导入模型
    四元数
    Ogre3D嵌入Qt框架
    如何搭建本地SVN服务
  • 原文地址:https://www.cnblogs.com/niulang/p/13838697.html
Copyright © 2020-2023  润新知