正如标题所述,如果我做了resp=httpx.get(url),如何获得url解析的IP?
关于如何使用requests来实现这一点,存在着This问题,但是httpx对象中似乎不存在相同的方法。
发布于 2022-10-03 10:50:38
从经验上来说,你可以用一个极其丑陋的咒语来做这件事,如下所示。
请注意,这涉及到不少于6个响应流的下划线-私有属性,以便在升级httpx时随时停止工作。(在httpx0.23.0中,这对我起了作用。)
import httpx
with httpx.Client() as hc:
r = hc.get("https://httpbin.org/get")
peername = r.stream._stream._httpcore_stream._stream._connection._network_stream._sock.getpeername()
print(r.json())
print(peername)这个打印出来
{'args': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Host': 'httpbin.org', 'User-Agent': 'python-httpx/0.23.0', 'X-Amzn-Trace-Id': 'Root=...'}, 'origin': '....', 'url': 'https://httpbin.org/get'}
('44.207.168.240', 443)https://stackoverflow.com/questions/73934230
复制相似问题