简体   繁体   English

如何在调试日志中查看 Python 请求使用哪个 IP 地址进行连接?

[英]How can I see in debug logs which IP address is used for connection by Python's request?

Let us say I am sending a GET request to an URL which's DNS record contains multiple IP addresses (DNS Round-Robin).假设我正在向一个 URL 发送 GET 请求,该 URL 的 DNS 记录包含多个 IP 地址(DNS 循环)。 For example, to the https://www.twitter.com .例如,到https://www.twitter.com If we do dig of it, we see:如果我们dig它,我们会看到:

;; ANSWER SECTION:
twitter.com.        60  IN  A   104.244.42.193
twitter.com.        60  IN  A   104.244.42.129

I have tried to use DEBUG logging as described here: https://stackoverflow.com/a/16630836/1479414 however it does not show which IP address it uses.我尝试使用此处描述的调试日志记录: https : //stackoverflow.com/a/16630836/1479414但是它没有显示它使用的 IP 地址。

Furthermore, I looked also at this answer: https://stackoverflow.com/a/22513161/1479414 about how to see the destination IP address, but it is not what I actually want to see -- this one shows me the finally used IP address.此外,我还查看了这个答案: https : //stackoverflow.com/a/22513161/1479414关于如何查看目标 IP 地址,但这不是我真正想要看到的——这个给我展示了最后使用的IP地址。

I want rather to see which IP addresses are tried during all attempts, while the library performs DNS failover.我更想查看在所有尝试中尝试了哪些 IP 地址,而库执行 DNS 故障转移。 Something like:就像是:

Trying to connect to 104.244.42.193... failed, trying to connect using another IP address

Is it possible to see somehow?有可能以某种方式看到吗?

The urllib3 library is responsible for this, in the urllib3.util.connection.create_connection() function . urllib3库在urllib3.util.connection.create_connection()函数中对此负责。 There's a loop there that checks each result from socket.getaddrinfo() :有一个循环检查socket.getaddrinfo()每个结果:

for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
    #  ....
    try:
        sock = socket.socket(af, socktype, proto)
        # ...
        sock.connect(sa)
        return sock

    except socket.error as e:
        # ...

Unfortunately, there is no logging used in this section.不幸的是,本节中没有使用日志记录。 If you must have logging, you'd have to copy the function source, add in the logging you need, then monkeypatch the urllib3 code to use your version instead.如果必须有日志记录,则必须复制函数源,添加所需的日志记录,然后对urllib3代码进行urllib3以使用您的版本。

The only location that uses that function uses it as an attribute on the imported module , so at least monkeypatching is as simple as assigning it back to original function location:唯一使用该函数的位置将其用作导入模块上的属性,因此至少monkeypatching 就像将其分配回原始函数位置一样简单:

import urllib3.util.connection

def create_connection_with_logging(
    address,
    timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
    source_address=None,
    socket_options=None,
):
    # ....

urllib3.util.connection.create_connection = create_connection_with_logging

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Python请求的请求标头中看到ip地址? - How can I see ip address in request header in Python requests? 如何查看 Python 中是否有可用且活动的网络连接? - How can I see if there's an available and active network connection in Python? 我怎样才能找到我的 ip 地址与 python 不是本地 ip? - how can i find my ip address with python not local ip? 如何使用 Python 从 Google Cloud Logging 获取请求 IP 地址? - How can i get request IP address from Google Cloud Logging using Python? 检查用于请求Python / Scrapy + ProxyMesh的IP地址 - Check IP address used for a request Python/Scrapy + ProxyMesh 如何使用 Python 的 PID 查看 python 正在读取或编辑哪些文件? - How can I see which files python is reading or editing, with Python's PID? 如何通过 Python 获得“正确”的 IP 地址 - How can I get the 'correct' IP-Address via Python 如何在python中将字符串转换为IP地址 - how can I convert a string to ip address in python 如何在python中遍历IP地址范围 - How can I loop through an IP address range in python 如何查看我的 Python 应用程序发送的整个 HTTP 请求? - How can I see the entire HTTP request that's being sent by my Python application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM