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.
scav
source share