couldn't access iter_contents()

Summary.

## Expected Result

What you expected.
Need to download the file
## Actual Result

What happened instead.
AttributeError: 'None Tye' object has no attribute 'Read'
## Reproduction Steps

```python
import requests
import requests.models
#url to login
url = 'xxxx.com'
payload = {'loginName': 'userName', 'password': '123456'}

r = requests.post(url, data=payload)
print r.headers
print r.cookies

url_dwnload="#download url"
payload = {'fileId': 2028, 'blockSize':1024}

resp_obj = requests.get(url_dwnload, cookies=r.cookies, params=payload, stream=True)
for c in resp_obj.cookies:

if c.name == 'DOWNLOAD_STATUS':
    if c.value == 'SUCCESS':
        with open(
                r'C:\Users\test\AppData\Local\SmartDocs\agent220.smartdocsonline.com\ruby.paul\tempfolder\6\New Text Document - Copy.txt.gz', 'wb') as f:
            for chunk in resp_obj.iter_content( chunk_size=1024):
                if chunk:
                    # print('###')
                    f.write(chunk)

```

## System Information

    $ python -m requests.help

```
{
  "chardet": {
    "version": "3.0.4"
  },
  "cryptography": {
    "version": ""
  },
  "idna": {
    "version": "2.6"
  },
  "implementation": {
    "name": "CPython",
    "version": "2.7.10"
  },
  "platform": {
    "release": "8",
    "system": "Windows"
  },
  "pyOpenSSL": {
    "openssl_version": "",
    "version": null
  },
  "requests": {
    "version": "2.18.4"
  },
  "system_ssl": {
    "version": "1000201f"
  },
  "urllib3": {
    "version": "1.22"
  },
  "using_pyopenssl": false
}
```

This command is only available on Requests v2.16.4 and greater. Otherwise,
please provide some basic information about your system (Python version,
operating system, &c).

I couldn't access iter_content() after importing requests when i called from visual studio professional 2015, but can be accessed if called from pycharm.

The feature template is there to make sure we get the information we need to help you. Could you fill out the entire template?

```py
import requests
import requests.models
#url to login
url = 'xxxx.com'
payload = {'loginName': 'userName', 'password': '123456'}

r = requests.post(url, data=payload)
print r.headers
print r.cookies

url_dwnload="#download url"
payload = {'fileId': 2028, 'blockSize':1024}

resp_obj = requests.get(url_dwnload, cookies=r.cookies, params=payload, stream=True)
for c in resp_obj.cookies:

    if c.name == 'DOWNLOAD_STATUS':
        if c.value == 'SUCCESS':
            with open(
                    r'C:\Users\test\AppData\Local\SmartDocs\agent220.smartdocsonline.com\ruby.paul\tempfolder\6\New Text Document - Copy.txt.gz', 'wb') as f:
                for chunk in resp_obj.iter_content( chunk_size=1024):
                    if chunk:
                        # print('###')
                        f.write(chunk)

l = requests.get(url_dwnload, cookies=r.cookies, params=payload, stream=True)
```

This is the code i was using

Can you follow each part of the template, including what the output of `python -m requests.help`?
Also what is the output and error you're receiving when you run your code?

Please check {
  "chardet": {
    "version": "3.0.4"
  },
  "cryptography": {
    "version": ""
  },
  "idna": {
    "version": "2.6"
  },
  "implementation": {
    "name": "CPython",
    "version": "2.7.10"
  },
  "platform": {
    "release": "8",
    "system": "Windows"
  },
  "pyOpenSSL": {
    "openssl_version": "",
    "version": null
  },
  "requests": {
    "version": "2.18.4"
  },
  "system_ssl": {
    "version": "1000201f"
  },
  "urllib3": {
    "version": "1.22"
  },
  "using_pyopenssl": false
}


As I asked above, what does your code output? This is the reason that the template exists, so that all of this information is given to people that want to help you. Please use it in the future.

Hi,
I am getting the output as below,
{'Content-Encoding': 'gzip', 'Transfer-Encoding': 'chunked', 'Set-Cookie':
'_app_vis_id_=77840FFCDD7B64EF18ECFE48A9517BC4; Path=/server/; Secure;
HttpOnly', 'Vary': 'Accept-Encoding', 'Server': 'SmartDocs Web Server',
'Connection': 'keep-alive', 'Date': 'Thu, 11 Jan 2018 15:23:43 GMT',
'Content-Type': 'application/json;charset=UTF-8'}
<RequestsCookieJar[<Cookie _app_vis_id_=77840FFCDD7B64EF18ECFE48A9517BC4
for agent220.smartdocsonline.com/server/>]>
Traceback (most recent call last):
  File "C:\Users\Krishna\Documents\Visual Studio
2015\Projects\PythonApplication2\PythonApplication2.py", line 20, in
<module>
    for chunk in resp_obj.iter_content( chunk_size=1024):
  File "C:\Python27\lib\site-packages\requests\models.py", line 745, in
generate
    for chunk in self.raw.stream(chunk_size, decode_content=True):
  File "C:\Python27\lib\site-packages\urllib3\response.py", line 432, in
stream
    for line in self.read_chunked(amt, decode_content=decode_content):
  File "C:\Python27\lib\site-packages\urllib3\response.py", line 601, in
read_chunked
    chunk = self._handle_chunk(amt)
  File "C:\Python27\lib\site-packages\urllib3\response.py", line 566, in
_handle_chunk
    returned_chunk = self._fp._safe_read(self.chunk_left)
  File "C:\Python27\lib\httplib.py", line 703, in _safe_read
    chunk = self.fp.read(min(amt, MAXAMOUNT))
AttributeError: 'NoneType' object has no attribute 'read'

On Thu, Jan 11, 2018 at 8:39 PM, Seth M. Larson <notifications@github.com>
wrote:

> Can you follow each part of the template, including what the output of python
> -m requests.help?
> Also what is the output and error you're receiving when you run your code?
>
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/requests/requests/issues/4461#issuecomment-356962121>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AJWIvdPVzJyKSuziqJzKWhpTj4WNj8Kqks5tJiQbgaJpZM4Ra-dA>
> .
>


Couldn't access the reported issue page

On Thu, Jan 11, 2018 at 8:52 PM, Krishna Prasad Puthiyattil <
pingme.kp@gmail.com> wrote:

> Hi,
> I am getting the output as below,
> {'Content-Encoding': 'gzip', 'Transfer-Encoding': 'chunked', 'Set-Cookie':
> '_app_vis_id_=77840FFCDD7B64EF18ECFE48A9517BC4; Path=/server/; Secure;
> HttpOnly', 'Vary': 'Accept-Encoding', 'Server': 'SmartDocs Web Server',
> 'Connection': 'keep-alive', 'Date': 'Thu, 11 Jan 2018 15:23:43 GMT',
> 'Content-Type': 'application/json;charset=UTF-8'}
> <RequestsCookieJar[<Cookie _app_vis_id_=77840FFCDD7B64EF18ECFE48A9517BC4
> for agent220.smartdocsonline.com/server/>]>
> Traceback (most recent call last):
>   File "C:\Users\Krishna\Documents\Visual Studio 2015\Projects\
> PythonApplication2\PythonApplication2.py", line 20, in <module>
>     for chunk in resp_obj.iter_content( chunk_size=1024):
>   File "C:\Python27\lib\site-packages\requests\models.py", line 745, in
> generate
>     for chunk in self.raw.stream(chunk_size, decode_content=True):
>   File "C:\Python27\lib\site-packages\urllib3\response.py", line 432, in
> stream
>     for line in self.read_chunked(amt, decode_content=decode_content):
>   File "C:\Python27\lib\site-packages\urllib3\response.py", line 601, in
> read_chunked
>     chunk = self._handle_chunk(amt)
>   File "C:\Python27\lib\site-packages\urllib3\response.py", line 566, in
> _handle_chunk
>     returned_chunk = self._fp._safe_read(self.chunk_left)
>   File "C:\Python27\lib\httplib.py", line 703, in _safe_read
>     chunk = self.fp.read(min(amt, MAXAMOUNT))
> AttributeError: 'NoneType' object has no attribute 'read'
>
> On Thu, Jan 11, 2018 at 8:39 PM, Seth M. Larson <notifications@github.com>
> wrote:
>
>> Can you follow each part of the template, including what the output of python
>> -m requests.help?
>> Also what is the output and error you're receiving when you run your code?
>>
>> —
>> You are receiving this because you authored the thread.
>> Reply to this email directly, view it on GitHub
>> <https://github.com/requests/requests/issues/4461#issuecomment-356962121>,
>> or mute the thread
>> <https://github.com/notifications/unsubscribe-auth/AJWIvdPVzJyKSuziqJzKWhpTj4WNj8Kqks5tJiQbgaJpZM4Ra-dA>
>> .
>>
>
>


@kpputhiyattil, the provided reproduction doesn’t appear to work as written. Did you delete the contents of your cookie for loop, or was the following content intended to be inside the cookie loop?

So the only way I can find to trigger this from the Requests API is to close the response while iterating the generator of a response sent with `Transfer-Encoding: chunked`. This will set the underlying file descriptor to None, causing your error.

```python
resp = requests.get(‘https://www.google.com/‘, stream=True)
for chunk in resp.iter_content(1024):
    resp.close()
    print(chunk)
```

Are you generating multiple generators or parallelizing any of your code across threads? Soemthing is closing your response mid-read which is the root cause of this error. If you aren’t doing either of the above things, could you please provide a *minmimal* reproduction script? I’m pretty sure this is external to Requests but want to verify we’re not accidentally closing something unintentionally. Thanks!

The issue is persisting only with visual studio 2015 when I try to debug.
Either if I run via Pycharm ide or I run in the visual studio without
debugging, I am getting the expected output. Any idea why it is so?

On Thu, Jan 11, 2018 at 11:32 PM, Nate Prewitt <notifications@github.com>
wrote:

> @kpputhiyattil <https://github.com/kpputhiyattil>, the provided
> reproduction doesn’t appear to work as written. Did you delete the contents
> of your cookie for loop, or was the following content intended to be inside
> the cookie loop?
>
> So the only way I can find to trigger this from the Requests API is to
> close the response while iterating the generator of a response sent with Transfer-Encoding:
> chunked. This will set the underlying file descriptor to None, causing
> your error.
>
> resp = requests.get(‘https://www.google.com/‘, stream=True)for chunk in resp.iter_content(1024):
>     resp.close()
>     print(chunk)
>
> Are you generating multiple generators or parallelizing any of your code
> across threads? Soemthing is closing your response mid-read which is the
> root cause of this error. If you aren’t doing either of the above things,
> could you please provide a *minmimal* reproduction script? I’m pretty
> sure this is external to Requests but want to verify we’re not accidentally
> closing something unintentionally. Thanks!
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <https://github.com/requests/requests/issues/4461#issuecomment-357010383>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AJWIvaxGgY5ExXAr__vbuknd982zLRdqks5tJkybgaJpZM4Ra-dA>
> .
>


@kpputhiyattil ok, that narrows things down a bit. I did some testing and it looks like this is reproducible with just urllib3 but not httplib by itself. This means, something in urllib3 is being interacted with by the debugger and closing the underlying socket file descriptor before we finish reading.

Since this isn't a bug in Requests, but our underlying library [urllib3](https://github.com/shazow/urllib3), I suggest we move this issue over there. We'll be able to determine if this is worth investigating and fixing at that level. Thanks @kpputhiyattil!

Here's a reproduction you can show in that issue. I was able to trigger it with the vscode debugger.

```python
import urllib3

http = urllib3.PoolManager()
resp = http.request('GET', 'https://www.google.com/')

for chunk in resp.stream(1024, decode_content=True):
    print(chunk)
```

Thank you so much for spending your time with us. One more query, what will
be the workaround for the issue until urlib3 fix the issue?

On Fri, Jan 12, 2018 at 11:11 AM, Nate Prewitt <notifications@github.com>
wrote:

> @kpputhiyattil <https://github.com/kpputhiyattil> ok, that narrows things
> down a bit. I did some testing and it looks like this is reproducible with
> just urllib3 but not httplib by itself. This means, something in urllib3 is
> being interacted with by the debugger and closing the underlying socket
> file descriptor before we finish reading.
>
> Since this isn't a bug in Requests, but our underlying library urllib3
> <https://github.com/shazow/urllib3>, I suggest we move this issue over
> there. We'll be able to determine if this is worth investigating and fixing
> at that level. Thanks @kpputhiyattil <https://github.com/kpputhiyattil>!
>
> Here's a reproduction you can show in that issue. I was able to trigger it
> with the vscode debugger.
>
> import urllib3
>
> http = urllib3.PoolManager()
> resp = http.request('GET', 'https://www.google.com/')
> for chunk in resp.stream(1024, decode_content=True):
>     print(chunk)
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <https://github.com/requests/requests/issues/4461#issuecomment-357148131>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AJWIvTJZ9CF-j123RILrSEelTeIIxC7cks5tJvB3gaJpZM4Ra-dA>
> .
>


I think I may have been a bit hasty with my last response. The above snippet triggers a separate but similar exception in urllib3 which will occur regardless of debugger use. Why it's encountering a different failure state when it's the same code Requests I have yet to determine.

The error you're encountering seems to be specific to at least vscode (I haven't tried on pycharm). Running the same script with pdb (`python -m pdb poc.py`) will allow for debugging without the exceptions. If you *need* to use the built in debugger for now, you may need to consider using the `content` property, or not passing `stream=True`. The latter will result in everything being loaded into memory though which may not be desired.

That's all I can advise right now until we determine what's going on in urllib3.

#### poc.py
```python
import requests

resp = requests.get('https://www.google.com/', stream=True)
for i in resp.iter_content(1024):
    print(1)
```

@nateprewitt your `urllib3` alone example is because you didn't do `http.request('GET', 'https://google.com', preload_content=False)` if I remember correctly so the content was already loaded into memory and streaming meant that you were trying to read from an httplib response that had already been set to None for garbage collection.

Why it happens only while debugging? Else it works smoothly..

On Jan 14, 2018 3:05 AM, "Ian Stapleton Cordasco" <notifications@github.com>
wrote:

> @nateprewitt <https://github.com/nateprewitt> your urllib3 alone example
> is because you didn't do http.request('GET', 'https://google.com',
> preload_content=False) if I remember correctly so the content was already
> loaded into memory and streaming meant that you were trying to read from an
> httplib response that had already been set to None for garbage collection.
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <https://github.com/requests/requests/issues/4461#issuecomment-357469035>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AJWIvV18u5f_KJ_hOCzMMb0i8CGyOlb8ks5tKSGrgaJpZM4Ra-dA>
> .
>