How to get the IP address of a client using aiohttp

Viewed 3212

I am currently working on a django project where I use aiohttp to communicate between the backend and frontend. I wanted to get the IP address of a client when a request is made from the frontend. Looked in different docs but none seems to point to exactly how to get the IP address using aiohttp. Someone Help!

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 Answers

Based on the aiohttp docs you can get the originating IP address of a client initiated HTTP request from requests remote parameter (request.remote).

Related