Advertisement
Guest User

Flask Gevent upload ValueError: I/O operation on closed file.

a guest
Mar 28th, 2022
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.25 KB | None | 0 0
  1. from flask import Flask
  2. from flask import copy_current_request_context
  3. from flask import request
  4. from flask import render_template
  5. import gevent
  6. from flask_socketio import SocketIO
  7.  
  8. app = Flask(__name__, static_url_path='/static', static_folder='./static')
  9. app.secret_key = "asdf"  # constants.SECRET_KEY
  10. socketio = SocketIO(app)
  11.  
  12.  
  13. @app.route("/",methods=["POST","GET"])
  14. def home():
  15.     with app.app_context():
  16.         @copy_current_request_context
  17.         def upload_files():
  18.             def upload_file(fileobj):
  19.                 print(fileobj.read())
  20.             print("Started")
  21.             selected_files = request.files.getlist('files[]')
  22.             print(selected_files)
  23.             for selected_file in selected_files:
  24.                 print("filename:", selected_file.filename)
  25.                 gevent.spawn(upload_file, selected_file)
  26.             print("Finished")
  27.  
  28.         if request.method == "POST":
  29.             upload_files()
  30.             # gevent.spawn(upload_files)  # raise ClientDisconnected()
  31.             # werkzeug.exceptions.ClientDisconnected: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
  32.     return render_template('index.html')
  33.  
  34. if __name__ == "__main__":
  35.     socketio.run(app, debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement