如何在django中获取用户IP地址? [英] How do I get user IP address in django?

查看:233
本文介绍了如何在django中获取用户IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我有这样的视图:

 #从django.contrib.gis.utils创建您的视图
从django.template导入GeoIP
从django.shortcuts导入RequestContext
导入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())

但是我得到这个错误:

  / mypage / 
中的KeyError REMOTE_ADDR'
请求方法:GET
请求URL:http://mywebsite.com/mypage/
Django版本:1.2.4
异常类型:KeyError
异常值:
'REMOTE_ADDR'
异常位置:/mysite/homepage/views.py在家里,第9行
Python可执行文件:/ usr / bin / python
Python版本:2.6.6
Python路径: ['/ 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' ]
服务器时间:Sun,2011年1月2日20:42:50 -0600


解决方案

  def get_client_ip(request):
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
如果x_forwarded_for:
ip = x_forwarded_for.split(',')[0]
else:
ip = request.META.get('REMOTE_ADDR')
return ip

确保您的反向代理(如果有)配置正确(例如

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



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

  get_client_ip(request)


How do I get user's IP in django?

I have a view like this:

# 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())

But I get this error:

KeyError at /mypage/
    'REMOTE_ADDR'
    Request Method: GET
    Request URL:    http://mywebsite.com/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

解决方案

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

Make sure you have reverse proxy (if any) configured correctly (e.g. mod_rpaf installed for Apache).

Note: the above uses the first item in X-Forwarded-For, but you might want to use the last item (e.g., in the case of Heroku: Get client's real IP address on Heroku)

And then just pass the request as argument to it;

get_client_ip(request)

这篇关于如何在django中获取用户IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆