0

Using request_toolbelt's MultipartEncoder, I am able to upload files to my Pyramid server, but only up to a certain size. Once the file are beyond a certain size (not sure of the exact size), requests throws an ConnectionError. My Pyramid server shows absolutely no sign of ever getting the request. The callback monitor shows 2 or 3 chunks read but then it aborts. Why?? Here is my code:

### client-side

# callback
def cb(monitor):
    print monitor.bytes_read

file = open('my_big_file.mpg', 'rb')
payload = MultipartEncoder({'uploadedFile': (file.name, file, 'application/octet-stream')})
monitor = MultipartEncoderMonitor(payload, cb)
r = requests.post(url, data=monitor, headers={'Content-Type': payload.content_type})

#### server-side

@view_config(route_name='remote.agent_upload', renderer='json')
def remote_agent_upload(request):
r = request.response
uploadedFile = request.POST['uploadedFile']
fs = uploadedFile.file
filename = uploadedFile.filename
f = open('path_to_storage' + filename, 'wb')
f.write(fs.read())
fs.close()
f.close()
return r

### output and traceback

8192
16384
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\Python27\lib\site-packages\requests\api.py", line 109, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "c:\Python27\lib\site-packages\requests\api.py", line 50, in request
    response = session.request(method=method, url=url, **kwargs)
  File "c:\Python27\lib\site-packages\requests\sessions.py", line 465, in request
    resp = self.send(prep, **send_kwargs)
  File "c:\Python27\lib\site-packages\requests\sessions.py", line 573, in send
    r = adapter.send(request, **kwargs)
  File "c:\Python27\lib\site-packages\requests\adapters.py", line 415, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', error(10053, 'An established connection was aborted by the software in your host machine'))
MFB
  • 19,017
  • 27
  • 72
  • 118

1 Answers1

1

Windows is closing the connection for an unspecified reason. See this other answer for more detail.

In short, this is neither the fault of requests, requests-toolbelt, or pyramid.

Community
  • 1
  • 1
Ian Stapleton Cordasco
  • 26,944
  • 4
  • 67
  • 72
  • Thank you, but it happens instantly (not a timeout), consistently (not a spurious network error) and only on "large" files (why would Windows discriminate). Any other ideas? – MFB May 27 '15 at 04:09
  • I've never seen that error or error code before. It's a Windows-only error that is specific to Windows terminating the connection. Does it happen when you make the same request to a different server (e.g., httpbin.org or requestb.in)? – Ian Stapleton Cordasco May 27 '15 at 04:19