Socket ResourceWarning using urllib in Python 3

I am using urllib.request.urlopen () to get from the web service that I am trying to check.

It returns an HTTPResponse object, which I then read () to get the body of the response.

But I always see ResourceWarning about an unclosed socket from socket.py

Here's the corresponding function:

from urllib.request import Request, urlopen def get_from_webservice(url): """ GET from the webservice """ req = Request(url, method="GET", headers=HEADERS) with urlopen(req) as rsp: body = rsp.read().decode('utf-8') return json.loads(body) 

Here's the warning that appears in the program output:

 $ ./test/test_webservices.py /Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/socket.py:359: ResourceWarning: unclosed <socket.socket object, fd=5, family=30, type=1, proto=6> self._sock = None .s ---------------------------------------------------------------------- Ran 2 tests in 0.010s OK (skipped=1) 

If there is anything I can do for an HTTPResponse (or request?) To close its socket cleanly, I would really like to know, because this code is for my unit tests; I don't like ignoring warnings anywhere, but especially not there.

+7
source share
1 answer

I do not know if this is the answer, but it is part of the answer path.

If I add the header "connection: close" in response from my web services, the HTTPResponse object seems to clear itself correctly without warning.

And actually, the HTTP Spec ( http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html ) says:

HTTP / 1.1 applications that do not support persistent connections MUST include a close option in each message.

So, the problem was on the server (i.e. my error!). In case you do not have control over the headers coming from the server, I do not know what you can do.

+3
source

All Articles