基于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)