flask和flask-restful接收file请求处理,以及压缩文件处理
code
import werkzeug from flask import Flask import tarfile from flask_restful import Resource, Api, reqparse from werkzeug.datastructures import FileStorage from werkzeug.utils import secure_filename import os app = Flask(__name__) api = Api(app) parser = reqparse.RequestParser() parser.add_argument('picture', type=werkzeug.datastructures.FileStorage, location='files') class HelloWorld(Resource): def post(self): args = parser.parse_args() content = args.get('picture') filename = secure_filename(content.filename) content.save(os.path.join('/home/zlp/', filename)) def extract(tar_path, target_path): try: tar = tarfile.open(tar_path, "r:gz") file_names = tar.getnames() for file_name in file_names: tar.extract(file_name, target_path) tar.close() except Exception as e: raise e extract('/home/zlp/{}'.format(filename), '/home/zlp/result') return {'hello': 'world'} api.add_resource(HelloWorld, '/') if __name__ == '__main__': app.run(debug=True)