Python-Requests close http connection

前端 未结 7 1359
既然无缘
既然无缘 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:53

    On Requests 1.X, the connection is available on the response object:

    r = requests.post("https://stream.twitter.com/1/statuses/filter.json",
                      data={'track': toTrack}, auth=('username', 'passwd'))
    
    r.connection.close()
    
    0 讨论(0)
  • 2020-11-27 03:53

    this works for me:

    res = requests.get(<url>, timeout=10).content
    requests.session().close()
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-27 03:57

    I think a more reliable way of closing a connection is to tell the sever explicitly to close it in a way compliant with HTTP specification:

    HTTP/1.1 defines the "close" connection option for the sender to signal that the connection will be closed after completion of the response. For example,

       Connection: close
    

    in either the request or the response header fields indicates that the connection SHOULD NOT be considered `persistent' (section 8.1) after the current request/response is complete.

    The Connection: close header is added to the actual request:

    r = requests.post(url=url, data=body, headers={'Connection':'close'})
    
    0 讨论(0)
  • 2020-11-27 04:03

    please use response.close() to close to avoid "too many open files" error

    for example:

    r = requests.post("https://stream.twitter.com/1/statuses/filter.json", data={'track':toTrack}, auth=('username', 'passwd'))
    ....
    r.close()
    
    0 讨论(0)
  • 2020-11-27 04:07

    As discussed here, there really isn't such a thing as an HTTP connection and what httplib refers to as the HTTPConnection is really the underlying TCP connection which doesn't really know much about your requests at all. Requests abstracts that away and you won't ever see it.

    The newest version of Requests does in fact keep the TCP connection alive after your request.. If you do want your TCP connections to close, you can just configure the requests to not use keep-alive.

    s = requests.session()
    s.config['keep_alive'] = False
    
    0 讨论(0)
提交回复
热议问题