Python-Requests close http connection

前端 未结 7 1415
既然无缘
既然无缘 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: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'})
    

提交回复
热议问题