(一)请求方不用发任何东西,响应方返回图片
#响应方
from flask import Flask, send_file
app = Flask(__name__)
@app.route('/get_image', methods=['GET']) #GET请求
def get_image():
# 读取图片文件
image_path = 'result/00006_00_00006_00.png' #要改为你实际的图片路径
# 发送图片作为响应
return send_file(image_path, mimetype='image/jpeg') #jpeg代表以jpeg格式传送,你也可以改为jpg,png等等
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000) #0.0.0.0代表对所有主机开放,port=5000代表端口号为5000,你也可以改成1024-65535中的任何一个端口
#请求方
import requests
def get_image():
url = 'http://localhost:5000/get_image' #localhost:5000代表ip为本机ip,端口号为5000,要替换为你实际的ip和端口号
#这里的/get_image对应响应方的/get_image,两者要同名
# 发送GET请求
response = requests.get(url)
# 检查请求是否成功
if response.status_code == 200:
# 保存图片
with open('received_image.jpg', 'wb') as f: #即使响应方传送的是jpeg格式,你也可以保存为jpg,png格式
f.write(response.content)
print("图片已保存")
else:
print("请求失败")
if __name__ == '__main__':
get_image()