2

I am using django-ipware for getting the Public IP of the user

https://github.com/un33k/django-ipware

My site is hosted on by virtual machine with djnago , mod_wsgi , apache

This is my code

    g = GeoIP()
    ip_address = get_ip_address_from_request(self.request)
    raise Exception(ip_address)

It gave me 127.0.0.1

I am accessing it from my other computer on same network.

how can i get my public ip

I also tried this as well

PRIVATE_IPS_PREFIX = ('10.', '172.', '192.', )

def get_client_ip(request):
"""get the client ip from the request
"""
remote_address = request.META.get('REMOTE_ADDR')
# set the default value of the ip to be the REMOTE_ADDR if available
# else None
ip = remote_address
# try to get the first non-proxy ip (not a private ip) from the
# HTTP_X_FORWARDED_FOR
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
    proxies = x_forwarded_for.split(',')
    # remove the private ips from the beginning
    while (len(proxies) > 0 and
            proxies[0].startswith(PRIVATE_IPS_PREFIX)):
        proxies.pop(0)
    # take the first ip which is not a private one (of a proxy)
    if len(proxies) > 0:
        ip = proxies[0]

return ip

It returned me 192.168.0.10 my local computer ip

user1958218
  • 1,571
  • 3
  • 18
  • 28
  • 1. Read this [question][1] 2. Read documentation of VM [1]: http://stackoverflow.com/questions/4581789/how-do-i-get-user-ip-address-in-django – Alok Tiwari Jun 19 '13 at 03:30
  • @AlokTiwari i read that post already and i got my solution from that post . but could not find solution – user1958218 Jun 19 '13 at 05:24
  • 1
    You're not going to get your public IP on your lan because it is never going through that interface to be proxied. – DivinusVox Jun 19 '13 at 05:54
  • @DivinusVox so u mean , if i host that code on my VPS server online , then will my code work – user1958218 Jun 19 '13 at 08:12
  • Yes, if a router converts packet to another network, it adds itself as the host ip and appends a different header (usually HTTP_X_FORWARDED_FOR) to the packet saying where it originally came from. Because you're on a LAN, it's never bridging networks. – DivinusVox Jun 19 '13 at 08:15
  • If you hit an external host it will probably work fine. – DivinusVox Jun 19 '13 at 08:15

1 Answers1

1

django-ipware attempts to get the client's (e.g. browser) public (externally route-able) IP address and it fails to do so, therefore, it returns '127.0.0.1' (local loopback, IPv4) indicating the failure as per its documentation (version 0.0.1).

That happens since you have your server running on the same (private) network as your own local computer. (192.168.x.x private block)

You can upgrade to version django-ipware>=0.0.5 which supports both IPv4 & IPv6 and use it as below.

# if you want the real IP address (public and externally route-able)
from ipware.ip import get_real_ip
ip = get_real_ip(request)
if ip is not None:
   # your server got the client's real public ip address
else:
   # your server doesn't have a real public ip address for user


# if you want the best matched IP address (public and/or private)
from ipware.ip import get_ip
ip = get_ip(request)
if ip is not None:
   # your server got the client's real ip address
else:
   # your server doesn't have a real ip address for user

####### NOTE:
# A `Real` IP address is the IP address of the client accessing your server 
# and not that of any proxies in between.
# A `Public` IP address is an address that is publicly route-able on the internet.
Val Neekman
  • 17,692
  • 14
  • 63
  • 66