How to get base socket when using Python queries

I have a Python script that creates many short-lived concurrent connections using the requests library. I need to know the source port used by each connection, and I believe that for this I need access to the underlying socket. Is there any way to get this through the response object?

+5
source share
1 answer

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.

+3
source

All Articles