Websocket cannot trace client's IP? #11648
-
First Check
Commit to Help
Example Code################utils.py##########################################
import logging
logger = logging.getLogger("test")
sh = logging.StreamHandler()
sh.setLevel(logging.INFO)
sfmt = logging.Formatter(
"%(asctime)s - %(levelname)s [%(filename)s:%(lineno)d] - %(message)s",
)
sh.setFormatter(sfmt)
logger.addHandler(sh)
log = logger
############main.py#############
from fastapi import FastAPI, Request
from fastapi import WebSocket, WebSocketDisconnect
from utils import log
from users import ws
WS = FastAPI()
@WS.websocket('/')
async def host_client(request:Request, session: WebSocket):
log.warning('from', request.client.host)
...... DescriptionI cannot retrieve client's IP like in the http connection/request, just like in this page Operating SystemLinux Operating System DetailsNo response FastAPI Version0.111.0 Pydantic Version2.7.1 Python Version3.9.16 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Answered by
YuriiMotov
May 16, 2025
Replies: 1 comment
-
It can be done this way: from fastapi import FastAPI
from fastapi import WebSocket
WS = FastAPI()
@WS.websocket('/')
async def host_client(websocket: WebSocket):
print('from', websocket.client.host) |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
YuriiMotov
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It can be done this way: