[Django]-How do I get the client Remote Port number in a Django?

4👍

You can get the IP and PORT through WSGIRequest connected socket.

Django 1.11:

sock = request._stream.stream._sock
client_ip, port = sock.getpeername()

Django 2.1:

sock = request._stream.stream.raw._sock
client_ip, port = sock.getpeername()

Django 2.2 and 3.1:

sock = request._stream.stream.stream.raw._sock
client_ip, port = sock.getpeername()

UPDATE

if request.user.is_authenticated:
    sock = request._stream.stream.stream.raw._sock
    client_ip, port = sock.getpeername()

    logger.info(' LOG KAYDI : KULLANICI : %s IP : %s SERVER PORT : %s' % (request.user.username, client_ip, port))
👤NKSM

Leave a comment