Downloading gzipped files causes ChunkEncodingErrors, ProtocolErrors, and ConnectionResetErrors on Windows 10

I was trying to download a remote gzipped JSON file from my Win10 box using Python 3.5.1 and 2.7.11, and it kept erroring out. The remote content-encoding header is gzip. I tried using requests 2.9.2 and 2.10.0 on both versions of Python, installed in a virtualenv with pip. I tried the same code on Linux, it doesn't error out at all. I'm not sure if it's requests or Python which causes the problem, so I wanted to start here, since the problem definitely exists specifically on Windows.

With this code (chunk_size does not matter):

``` python
data = requests.get(url, stream=True)
with open(url.rsplit("/")[-1], "wb") as fh:
  for chunk in data.iter_content(chunk_size=1024):
    fh.write(chunk)
```

I would inconsistently get this trace back:

``` python
Traceback (most recent call last):
  File "H:\Programming\Python\virtualenvs\warehouse\lib\site-packages\requests\packages\urllib3\response.py", line 228, in _error_catcher
    yield
  File "H:\Programming\Python\virtualenvs\warehouse\lib\site-packages\requests\packages\urllib3\response.py", line 501, in read_chunked
    chunk = self._handle_chunk(amt)
  File "H:\Programming\Python\virtualenvs\warehouse\lib\site-packages\requests\packages\urllib3\response.py", line 461, in _handle_chunk
    value = self._fp._safe_read(amt)
  File "C:\Users\Mike\AppData\Local\Programs\Python\Python35\Lib\http\client.py", line 592, in _safe_read
    chunk = self.fp.read(min(amt, MAXAMOUNT))
  File "C:\Users\Mike\AppData\Local\Programs\Python\Python35\Lib\socket.py", line 575, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "H:\Programming\Python\virtualenvs\warehouse\lib\site-packages\requests\models.py", line 664, in generate
    for chunk in self.raw.stream(chunk_size, decode_content=True):
  File "H:\Programming\Python\virtualenvs\warehouse\lib\site-packages\requests\packages\urllib3\response.py", line 349, in stream
    for line in self.read_chunked(amt, decode_content=decode_content):
  File "H:\Programming\Python\virtualenvs\warehouse\lib\site-packages\requests\packages\urllib3\response.py", line 526, in read_chunked
    self._original_response.close()
  File "C:\Users\Mike\AppData\Local\Programs\Python\Python35\Lib\contextlib.py", line 77, in __exit__
    self.gen.throw(type, value, traceback)
  File "H:\Programming\Python\virtualenvs\warehouse\lib\site-packages\requests\packages\urllib3\response.py", line 246, in _error_catcher
    raise ProtocolError('Connection broken: %r' % e, e)
requests.packages.urllib3.exceptions.ProtocolError: ("Connection broken: ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None)", ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "H:/Programming/Python/warehouse/main.py", line 55, in <module>
    compile_auctions(slugs)
  File "H:/Programming/Python/warehouse/main.py", line 44, in compile_auctions
    for chunk in data.iter_content(chunk_size=1024):
  File "H:\Programming\Python\virtualenvs\warehouse\lib\site-packages\requests\models.py", line 667, in generate
    raise ChunkedEncodingError(e)
requests.exceptions.ChunkedEncodingError: ("Connection broken: ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None)", ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))
```


That traceback just indicates that the remote peer decided to stop sending data for no reason that it could communicate to us: we were expecting more data, and we didn't get any. In this case, it's right in the middle of a content chunk.

That doesn't really give us much to go with. The server could really be doing anything it wants. Do you happen to have access to server logs for this server?


No, sorry, I don't. I'm filing the bug report because this issue doesn't happen on Linux, using the exact same code. It very specifically happens on Windows. I don't know if Requests handles Windows differently than Linux, so I'd like to make the determination of whether this is a bug with Requests or a bug with Python on Windows.


Requests does _very little_ differently on Windows, at least directly. However, there are a number of things that may accidentally differ between Windows and Linux. For example, OpenSSL versions are different, as are zlib versions. Even Python versions are usually different. This makes it extremely difficult to work out, at a surface level, what's going wrong.

Are you using HTTPS?


Also, are you working through proxies on the Windows machine?


Closing for inactivity.


I'm having a similar issue while using request when I run using python3.6 after few mins I get a response saying connection broken. if I run with python2.7 I get the expected output. I was able to get the expected output using postman, using curl but not with python 3.6 or 3.7. 

Yes, it's an HTTPS server.

Any help is appreciated.