Django总是在我的si上给我本地ip

2024-06-10 04:36:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用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

我也试过这个

^{pr2}$

它返回给我192.168.0.10我的本地计算机ip


Tags: django用户httpsipgithubcommod网站
1条回答
网友
1楼 · 发布于 2024-06-10 04:36:29

django ipware试图获取客户端(例如浏览器)的公共(可外部路由)IP地址,但未能获得,因此,它返回“127.0.0.1”(本地环回,IPv4),根据其文档(版本0.0.1)指示失败。在

这是因为您的服务器与您自己的本地计算机运行在同一个(专用)网络上。(192.168.x.x专用块)

您可以升级到django ipware>;=0.0.5版本,该版本同时支持IPv4和IPv6,并按如下方式使用它。在

# 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.

相关问题 更多 >