1

I am trying to understand more precisely how Http connections work with Flask, so I tried writing a very simple app and another simple connection with requests and requests-toolbelt :

app = Flask('file-streamer')

@app.route("/uploadDumb", methods=["POST"])
def upload_dumb():
    print("Hello")
    return Response(status=200)

So basically this server should just receive a request and return a response.

Then I implemented a simple piece of code that sends requests with toolbelt :

import requests 
from requests_toolbelt.multipart import encoder

values = {"file": ("test.zip", open("test.zip", "rb"), "application/zip"), "test": "hello"}

m = encoder.MultipartEncoder(fields=values)

r = requests.post(url="http://localhost:5000/uploadDumb", data=m, headers={"Content-Type": m.content_type})

The file I'm sending is a pretty large file that I want to upload with streaming. The thing is, I expected the Flask server to wait for the whole file to be sent (even if the file is useless), then return a response, but that's not what's happening. Actually, Flask responds at the very beginning of the sending process, returns a 200 response, which causes the 'requests' side to end with a "BrokenPipeError".

Could someone explain to me what is happening there ?

Jedyle
  • 61
  • 2
  • I am not sure why the pipe is being broken, however think that without streaming every single file upload goes to memory first, so if your server has 256 MB and a few of your clients are uploading 128 MB files you run out of memory very quickly, check http://izmailoff.github.io/web/flask-file-streaming/ for receiving in chucks using streams. – Danilo Cabello Feb 08 '19 at 14:50
  • Thansk for your answer, but the problem occurs with any file larger than a few kilobytes. – Jedyle Feb 08 '19 at 15:14
  • If it can help, I tried the same thing with a minimal django app and everything is fine, it waits for the file to be downloaded. – Jedyle Feb 08 '19 at 16:41

0 Answers0