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

标签 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()

最佳答案

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

关于python - 如何使用aiohttp获取客户端的IP地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58452087/

相关文章:

python - 在 Python 中将字母转换为电话号码的数字

django - Django View 中的访问路径url参数

node.js - 空闲 WebSocket 的开销

java - 为什么以下 websocket 代码(dukeetf2)不起作用?

python - Django Rest框架在POST请求中创建或更新

python - Apache Airflow 导入错误 : cannot import name '_psutil_linux'

python - Django ModelBackend.authenticate 如何工作?

go - Socket.io 客户端无法与 websocket 服务器一起使用

python仍然无法插入数据库

django - django中post_save接收器的顺序是什么?