python - 确定异步套接字服务器中客户端的主机名

标签 python python-asyncio

看起来有一种方法可以使用 socket.getpeername() 获取普通旧套接字的标识符。但是,使用 asyncio.start_server 时,似乎没有任何方法可以访问套接字。有没有办法获得异步套接字服务器客户端的唯一标识符(例如IP地址)?

async def client_handler(reader: StreamReader, writer: StreamWriter):
    print("Client connected at {some_ip_address_or_identifier}")  # ?

async def main():
    IP = "0.0.0.0"
    PORT = 8080
    loop = asyncio.get_event_loop()
    server = await asyncio.start_server(client_handler, IP, PORT)
    await server.serve_forever()

asyncio.run(main())

最佳答案

你可以这样做:

addr =  writer.get_extra_info('peername')

根据docs

关于python - 确定异步套接字服务器中客户端的主机名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59130861/

相关文章:

python - Unicode编码错误: 'charmap' codec can't encode characters in position 1082-1084: character maps to <undefined>

Python:替换 CSV 文件中的数据

python - 如何在Python中从 future 检索任务?

python - 使用依赖项测试异步 FastAPI 端点

Python multiprocess.Pool.map 不能处理大数组。

python - 如何为不同的 Perl 应用程序安装专门的环境?

python-3.x - 为什么本地协程的 yield 语法是有效的

python-3.x - 通过对 Python 3.6 Flask 应用程序中的路由的 HTTP 请求启动非阻塞异步函数调用

python-asyncio - 在 asyncio 中混合异步上下文管理器和直接等待

python - 如何在 python smtplib 模块中设置主机地址?