如何在 Django 中获取用户 IP 地址?

新手上路,请多包涵

如何在 Django 中获取用户的 IP?

我有这样的看法:

 # Create your views
from django.contrib.gis.utils import GeoIP
from django.template import  RequestContext
from django.shortcuts import render_to_response

def home(request):
  g = GeoIP()
  client_ip = request.META['REMOTE_ADDR']
  lat,long = g.lat_lon(client_ip)
  return render_to_response('home_page_tmp.html',locals())

但我收到此错误:

 KeyError at /mypage/
    'REMOTE_ADDR'
    Request Method: GET
    Request URL:    http://mywebsite.example/mypage/
    Django Version: 1.2.4
    Exception Type: KeyError
    Exception Value:
    'REMOTE_ADDR'
    Exception Location: /mysite/homepage/views.py in home, line 9
    Python Executable:  /usr/bin/python
    Python Version: 2.6.6
    Python Path:    ['/mysite', '/usr/local/lib/python2.6/dist-packages/flup-1.0.2-py2.6.egg', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/local/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages', '/usr/lib/pymodules/python2.6']
    Server time:    Sun, 2 Jan 2011 20:42:50 -0600

原文由 avatar 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 986
2 个回答
def get_client_ip(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[0]
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip

确保您正确配置了反向代理(如果有)(例如 mod_rpaf 为 Apache 安装)。

注意:以上使用 X-Forwarded-For 中的 第一 项,但您可能想使用 最后 一项(例如,在 Heroku 的情况下:在 Heroku 上获取客户端的真实 IP 地址

然后将请求作为参数传递给它;

 get_client_ip(request)

HttpRequest.META 的 Django 文档

原文由 yanchenko 发布,翻译遵循 CC BY-SA 4.0 许可协议

您可以使用支持 Python 23 并处理 IPv4IPv6 的 django-ipware

安装:

pip install django-ipware

简单用法:

 # In a view or a middleware where the `request` object is available

from ipware import get_client_ip
ip, is_routable = get_client_ip(request)
if ip is None:
    # Unable to get the client's IP address
else:
    # We got the client's IP address
    if is_routable:
        # The client's IP address is publicly routable on the Internet
    else:
        # The client's IP address is private

# Order of precedence is (Public, Private, Loopback, None)

高级用法:

  • Custom Header - ipware 要查看的自定义请求标头:
   i, r = get_client_ip(request, request_header_order=['X_FORWARDED_FOR'])
  i, r = get_client_ip(request, request_header_order=['X_FORWARDED_FOR', 'REMOTE_ADDR'])

  • 代理计数 - Django 服务器在固定数量的代理后面:
   i, r = get_client_ip(request, proxy_count=1)

  • 可信代理——Django 服务器在一个或多个已知和可信代理之后:
   i, r = get_client_ip(request, proxy_trusted_ips=('177.2.2.2'))

  # For multiple proxies, simply add them to the list
  i, r = get_client_ip(request, proxy_trusted_ips=('177.2.2.2', '177.3.3.3'))

  # For proxies with fixed sub-domain and dynamic IP addresses, use partial pattern
  i, r = get_client_ip(request, proxy_trusted_ips=('177.2.', '177.3.'))

注意: 阅读此 通知

原文由 Avid Coder 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题