How to get the underlying socket when using Python requests

北战南征 提交于 2020-01-10 19:31:07

问题


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


回答1:


For streaming connections (those opened with the stream=True parameter), you can call the .raw.fileno() method on the response object to get an open file descriptor.

You can use the socket.fromfd(...) method to create a Python socket object from the descriptor:

>>> 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-streaming sockets, the file descriptor is cleaned up before the response object is returned. As far as I can tell there's no way to get it in this situation.



来源:https://stackoverflow.com/questions/32310951/how-to-get-the-underlying-socket-when-using-python-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!