Python-Requests close http connection

前端 未结 7 1390
既然无缘
既然无缘 2020-11-27 03:14

I was wondering, how do you close a connection with Requests (python-requests.org)?

With httplib it\'s HTTPConnection.close(), but how do I

7条回答
  •  离开以前
    2020-11-27 03:56

    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
    

提交回复
热议问题