How to get the underlying socket when using Python requests

后端 未结 1 591
野的像风
野的像风 2021-01-02 10:38

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 conne

相关标签:
1条回答
  • 2021-01-02 11:14

    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.

    0 讨论(0)
提交回复
热议问题