client端:
import requests
import os
path = '/home/users/project/data/"
url = 'http://192.168.xx.xx:8000/api_interface'
for file in os.listdir(path):
resp = requests.post(url, files={"file1": open(os.path.join(path, file), 'rb'),
"file2": open(os.path.join(path, file), 'rb'), "file3": open(os.path.join(path, file), 'rb'), "file3": open(os.path.join(path, file), 'rb')}
print(resp.json())
server端:
from flask import Flask, jsonify, request
import io
from PIL import Image
app = Flask(__name__)
batch_size = 16
@app.route('/api_interface', methods=['POST'])
def api_interface():
if request.method == 'POST':
try:
files = [request.files[item] for item in request.files]
if len(files) > batch_size:
return jsonify({'error': batchsize should less than 16})
img_bytes = [file.read() for file in files]
images = [Image.open(io.BytesIO(image_byte)).convert('RGB') for image_byte in img_bytes]
# 处理代码
# .......
output = ...
return jsonify(output)