How do I get user IP address in django?

后端 未结 11 1157
野趣味
野趣味 2020-11-22 15:33

How do I get user\'s IP in django?

I have a view like this:

# Create your views
from django.contrib.gis.utils import GeoIP
from django.template impor         


        
11条回答
  •  悲哀的现实
    2020-11-22 16:21

    In django.VERSION (2, 1, 1, 'final', 0) request handler

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

    if you call above code twice,you may got

    AttributeError("'_io.BytesIO' object has no attribute 'stream'",)

    AttributeError("'LimitedStream' object has no attribute 'raw'")

提交回复
热议问题