For streaming connections (those that are open with the stream=True parameter), you can call the .raw.fileno() method on the response object to get a handle to the open file.
You can use the socket.fromfd(...) method to create a Python socket object from a handle:
>>> import requests >>> import socket >>> r = requests.get('http://google.com/', stream=True) >>> s = socket.fromfd(r.raw.fileno(), socket.AF_INET, socket.SOCK_STREAM) >>> s.getpeername() ('74.125.226.49', 80) >>> s.getsockname() ('192.168.1.60', 41323)
For non-stream sockets, the file descriptor is cleared before the response object is returned. As far as I can tell, there is no way to get this in this situation.
source share