0

Is there a way to know the lan IP request client?. I am work in django, I tried this:

 def get_ip_test():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        # doesn't even have to be reachable
        s.connect(('8.8.8.8', 0))
        IP = s.getsockname()[0]
    except:
        IP = '127.0.0.1'
    finally:
        s.close()
    return IP

But this return IP from server, I saw in javascript an example to get LAN IP example javascript

Many thanks for your help.

oscar
  • 15
  • 3

1 Answers1

0

Works for me:

>>> s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
>>> s.connect(('8.8.8.8',0))
>>> s.getsockname()[0]
'192.168.10.2'
Solar.gy
  • 857
  • 8
  • 11
  • Its work, but if you put on a server web ( web.com/get_ip ), you get the Public IP from client. – oscar Apr 17 '17 at 15:39
  • Can you better define which IP you are trying to obtain? It sounds like you have a django server and several web clients. Do you want 1) django server LAN IP 2) django server public IP 3) client LAN IP 4) client public IP? – Solar.gy Apr 17 '17 at 21:22
  • Unfortunately the client's LAN IP is not available by querying a standard socket. Web servers in general do not have access their clients' LAN IP, although [a new HTML5 extension](http://stackoverflow.com/a/26850789/7089847) just added support for obtaining it. – Solar.gy Apr 17 '17 at 22:00
  • To clarify my last comment: you can do something custom, such as writing client-side code (javascript) to get the local IP and post it to the server. – Solar.gy Apr 17 '17 at 22:08
  • thanks solar, for the moment WebRTC works only in firefox and chrome. – oscar Apr 17 '17 at 22:49