Home > Backend Development > Python Tutorial > Python使用django获取用户IP地址的方法

Python使用django获取用户IP地址的方法

WBOY
Release: 2016-06-10 15:12:49
Original
2035 people have browsed it

本文实例讲述了Python使用django获取用户IP地址的方法。分享给大家供大家参考。具体如下:

函数实现:

def get_client_ip(request):
try:
  real_ip = request.META['HTTP_X_FORWARDED_FOR']
  regip = real_ip.split(",")[0]
except:
  try:
    regip = request.META['REMOTE_ADDR']
  except:
    regip = ""
return regip
Copy after login

问题是会在有的地方不好使,检测发现request.META根本无key值等于HTTP_X_FORWARDED_FOR和REMOTE_ADDR。服务器采用nginx+fastcgi实现。

解决办法:

在nginx的fastcgi配置中加入如下:

fastcgi_param REMOTE_ADDR $remote_addr;

希望本文所述对大家的Python程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template