繁体   English   中英

如何从 aiohttp 响应中获取 IP 地址

[英]How to get IP address from aiohttp response

我一直在尝试查看来自 aiohttp 请求的响应,但没有找到获取指定主机 IP 地址的方法:

async with aiohttp.ClientSession() as session:
    async with session.get(f'http://{host}') as response:
        print(response.__dir__())
        #Wanting to get ipaddress of response right here

我假设您的主机由 url 表示(否则您已经拥有 IP)

所以你需要做的是通过url获取一个ip地址。
您可以通过以下方式做到这一点:

import socket
print socket.gethostbyname('your hosto name')

您的主机名可能是“http://{host}”

您可以使用:

async with aiohttp.ClientSession() as session:
    async with session.get(f'http://{host}') as response:
        print(response.connection.transport.get_extra_info('peername'))

资源

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM