我正在使用 django-ipware 获取用户的公共 IP
https://github.com/un33k/django-ipware
我的网站由虚拟机托管djnago , mod_wsgi , apache
这是我的代码
g = GeoIP()
ip_address = get_ip_address_from_request(self.request)
raise Exception(ip_address)
它给了我127.0.0.1
我正在从同一网络上的另一台计算机访问它。
我怎样才能得到我的公共IP
我也试过这个
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
它返回了192.168.0.10
我的本地计算机 IP