如何使用aiohttp获取客户端的IP地址

时间:2019-10-18 13:40:20

标签: python django websocket aiohttp

我目前正在django项目中工作,我使用aiohttp在后端和前端之间进行通信。我想从前端发出请求时获取客户端的IP地址。在不同的文档中查看过,但似乎都没有指向如何使用aiohttp获取IP地址。有人帮忙!

from aiohttp import web


async def handler(request):

    ws = web.WebSocketResponse()
    await ws.prepare(request)

    try:
        async for msg in ws:
            # handle incoming messages
            # use ws.send_str() to send data back
            ...

    finally:
        task.cancel()

1 个答案:

答案 0 :(得分:2)

基于aiohttp docs,您可以从请求remote参数(request.remote)获取客户端发起的HTTP请求的原始IP地址。