I was wondering, how do you close a connection with Requests (python-requests.org)?
With httplib
it\'s HTTPConnection.close()
, but how do I
I came to this question looking to solve the "too many open files" error
, but I am using requests.session()
in my code. A few searches later and I came up with an answer on the Python Requests Documentation which suggests to use the with
block so that the session is closed even if there are unhandled exceptions:
with requests.Session() as s:
s.get('http://google.com')
If you're not using Session you can actually do the same thing: https://2.python-requests.org/en/master/user/advanced/#session-objects
with requests.get('http://httpbin.org/get', stream=True) as r:
# Do something