How to get server IP from Httpx get response #2633
-
How should I get the server IP address from the Httpx get response? 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) Is there a better way? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Answering my own question... The trio library provides an async sockets implementation. I will use that. |
Beta Was this translation helpful? Give feedback.
-
There is a better way, yes... That answer is digging into private API, so don't expect it to be resilient. We do provide a response extension which gives a public API onto this information. See issue #2599 Adapting your example to include the server address info... import httpx
with httpx.Client() as client:
with client.stream("GET", "https://httpbin.org/get") as response:
network_stream = response.extensions["network_stream"]
server_addr = network_stream.get_extra_info("server_addr")
print(response.json())
print(server_addr) Notice that we're using |
Beta Was this translation helpful? Give feedback.
-
Many thanks! |
Beta Was this translation helpful? Give feedback.
There is a better way, yes...
That answer is digging into private API, so don't expect it to be resilient.
We do provide a response extension which gives a public API onto this information.
It is currently only documented in the low level
httpcore
library docs, here... https://www.encode.io/httpcore/extensions/#extra-network-informationBut we should also document it, since it's available through
httpx
too.See issue #2599
Adapting your example to include the server address info...