Search code examples
flaskpython-3.6picklepem

How to use pickle.load in flask app - Python3


@app.route('/', methods=['POST'])
def upload_file():
if request.method == 'POST':

    if 'files[]' not in request.files:
        flash('No file part')
        return redirect(request.url)

    files = request.files.getlist('files[]')

    for file in files:
        if file and allowed_file(file.filename):
            #print(file.filename)
            filename = secure_filename(file.filename)
            encrypted_list = pickle.load(open(file, "rb"))
            print(encrypted_list)

I am having some string whose base64 encoding I have stored in .pem files and idea is X person will upload multiple .pem files in the flask app and I don't want to save them in disk, just read those .pem files using pickle.load(open(file, "rb")) but this command is giving me error. As while encrypting I have stored those base64 encoded string in .pem files. Now I want to decode those string from .pem files in Flask.

Any help will be appreciated and thanks in advance!!


Solution

  • As the error message suggests, you cannot pass a FileStorage instance into pickle's load function.

    Instead, you should pass in a file like object, like so encrypted_list = pickle.load(file.stream).

    https://werkzeug.palletsprojects.com/en/1.0.x/datastructures/#werkzeug.datastructures.FileStorage