Repository navigation
Replies: 1 comment
|
The optional network_stream response extension exposes the connected peer through get_extra_info("server_addr"). Read it while the stream is open: import ipaddress
def peer_ip(response):
stream = response.extensions.get("network_stream")
if stream is None:
return None
peer = stream.get_extra_info("server_addr")
if peer is None:
return None
address = ipaddress.ip_address(peer[0])
return address.version, str(address)
# with client.stream("GET", url) as response:
# print(peer_ip(response))
# response.read()For a direct TCP connection this identifies the peer address family (4 or 6), not the complete DNS answer set. A successful IPv4 request does not prove IPv6 is unavailable. With a proxy the socket peer can be the proxy, so this cannot establish the origin server's address family. In-process/custom transports may have no network_stream extension at all. Avoid depending on the socket remaining usable after the response/client has closed it. Validated a real loopback IPv4 request, plus IPv4/IPv6 peer tuples and the absent-extension case with controlled stream doubles. A real IPv6 connection was not tested. Network stream extension. Validation: HTTPX 0.28.1, Python 3.11.9. |
Uh oh!
There was an error while loading. Please reload this page.
I'm interested in having a way to know if a response was returned via IPv4 or IPv6, to know if the user has IPv6 connectivity or not. This may have some grey areas, like when using a proxy, but otherwise should be somewhat predictable.
This could be something like
Response.connection, an object with details about the connection used to receive the response. OrResponse.host, the IP address of the host which it communicated.Not sure how difficult this would be, nor how applicable to other users, so I figured I should ask about it here.
All reactions