簡體   English   中英

如何在 Django 中獲取用戶 IP 地址?

[英]How do I get user IP address in Django?

如何在 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
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

確保您正確配置了反向代理(如果有)(例如為 Apache 安裝了mod_rpaf )。

注意:上面使用X-Forwarded-For中的第一項,但您可能希望使用最后一項(例如,在 Heroku 的情況下: Get client's real IP address on Heroku

然后將請求作為參數傳遞給它;

get_client_ip(request)

HttpRequest.META 的 Django 文檔

您可以使用支持 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)

高級用法:

  • 自定義標頭 - 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.'))

注意:閱讀本通知

Alexander 的回答很好,但缺少對有時在 HTTP_X_FORWARDED_FOR 標頭中返回多個 IP 的代理的處理。

真實 IP 通常位於列表末尾,如下所述: http ://en.wikipedia.org/wiki/X-Forwarded-For

解決方案是簡單修改 Alexander 的代碼:

def get_client_ip(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[-1].strip()
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip

不再混淆在最近的 Django 版本中,明確提到客戶端的 IP 地址可在

request.META.get("REMOTE_ADDR")

有關更多信息,請查看Django 文檔

我想建議對 yanchenko 的回答進行改進。

我沒有取 X_FORWARDED_FOR 列表中的第一個 ip,而是取第一個不屬於已知內部 ip 的 IP,因為某些路由器不遵守協議,您可以將內部 ips 視為列表的第一個值。

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

我希望這對有同樣問題的谷歌同事有所幫助。

這是一個簡短的班輪來實現這一點:

request.META.get('HTTP_X_FORWARDED_FOR', request.META.get('REMOTE_ADDR', '')).split(',')[0].strip()

在我的情況下,以上都不起作用,所以我必須檢查uwsgi + django源代碼並在 nginx 中傳遞靜態參數,看看為什么/如何,下面是我發現的。

環境信息:
蟒蛇版本: 2.7.5
Django 版本: (1, 6, 6, 'final', 0)
nginx版本: nginx/1.6.0
uwsgi: 2.0.7

環境設置信息:
nginx 作為反向代理監聽80端口 uwsgi 作為上游 unix 套接字,最終將響應請求

Django 配置信息:

USE_X_FORWARDED_HOST = True # with or without this line does not matter

nginx配置:

uwsgi_param      X-Real-IP              $remote_addr;
// uwsgi_param   X-Forwarded-For        $proxy_add_x_forwarded_for;
// uwsgi_param   HTTP_X_FORWARDED_FOR   $proxy_add_x_forwarded_for;

// hardcode for testing
uwsgi_param      X-Forwarded-For        "10.10.10.10";
uwsgi_param      HTTP_X_FORWARDED_FOR   "20.20.20.20";

獲取 django 應用程序中的所有參數:

X-Forwarded-For :       10.10.10.10
HTTP_X_FORWARDED_FOR :  20.20.20.20

結論:

所以基本上,你必須在 nginx 中指定完全相同的字段/參數名稱,並在 django 應用程序中使用request.META[field/param]

現在您可以決定是添加中間件(攔截器)還是僅在某些視圖中解析HTTP_X_FORWARDED_FOR

最簡單的解決方案(如果您使用的是 fastcgi+nignx)是 itgorilla 評論的:

謝謝你提出這個好問題。 我的 fastcgi 沒有通過 REMOTE_ADDR 元鍵。 我在 nginx.conf 中添加了以下行並修復了問題:fastcgi_param REMOTE_ADDR $remote_addr; – 伊大猩猩

Ps:我添加這個答案只是為了讓他的解決方案更加明顯。

最初從 Django 中刪除該功能的原因是標頭最終不能被信任。 原因是它很容易被欺騙。 例如,配置 Nginx 反向代理的推薦方法是:

add_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header X-Real-Ip       $remote_addr;

當你這樣做時:

curl -H 'X-Forwarded-For: 8.8.8.8, 192.168.1.2' http://192.168.1.3/

您在myhost.example中的 Nginx 將繼續發送:

X-Forwarded-For: 8.8.8.8, 192.168.1.2, 192.168.1.3

如果您盲目地按照說明操作, X-Real-IP將是第一個代理的 IP。

如果信任您的用戶是一個問題,您可以嘗試類似django-xffhttps ://pypi.python.org/pypi/django-xff/

我在上面的答案中也缺少代理。 我使用了get_ip_address_from_requestget_ip_address_from_request

from easy_timezones.utils import get_ip_address_from_request, is_valid_ip, is_local_ip
ip = get_ip_address_from_request(request)
try:
    if is_valid_ip(ip):
        geoip_record = IpRange.objects.by_ip(ip)
except IpRange.DoesNotExist:
    return None

這里是get_ip_address_from_request方法,准備好 IPv4 和 IPv6:

def get_ip_address_from_request(request):
    """ Makes the best attempt to get the client's real IP or return the loopback """
    PRIVATE_IPS_PREFIX = ('10.', '172.', '192.', '127.')
    ip_address = ''
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR', '')
    if x_forwarded_for and ',' not in x_forwarded_for:
        if not x_forwarded_for.startswith(PRIVATE_IPS_PREFIX) and is_valid_ip(x_forwarded_for):
            ip_address = x_forwarded_for.strip()
    else:
        ips = [ip.strip() for ip in x_forwarded_for.split(',')]
        for ip in ips:
            if ip.startswith(PRIVATE_IPS_PREFIX):
                continue
            elif not is_valid_ip(ip):
                continue
            else:
                ip_address = ip
                break
    if not ip_address:
        x_real_ip = request.META.get('HTTP_X_REAL_IP', '')
        if x_real_ip:
            if not x_real_ip.startswith(PRIVATE_IPS_PREFIX) and is_valid_ip(x_real_ip):
                ip_address = x_real_ip.strip()
    if not ip_address:
        remote_addr = request.META.get('REMOTE_ADDR', '')
        if remote_addr:
            if not remote_addr.startswith(PRIVATE_IPS_PREFIX) and is_valid_ip(remote_addr):
                ip_address = remote_addr.strip()
    if not ip_address:
        ip_address = '127.0.0.1'
    return ip_address

在 django.VERSION (2, 1, 1, 'final', 0) 請求處理程序中

sock=request._stream.stream.raw._sock
#<socket.socket fd=1236, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('192.168.1.111', 8000), raddr=('192.168.1.111', 64725)>
client_ip,port=sock.getpeername()

如果你兩次調用上面的代碼,你可能會得到

AttributeError("'_io.BytesIO' 對象沒有屬性 'stream'",)

AttributeError("'LimitedStream' 對象沒有屬性 'raw'")

只需添加

{{ request.META.REMOTE_ADDR }}

在 Django-Template 中,您希望用戶看到他們的 IP 地址。 也就是說,如果您對將其保存到數據庫不感興趣。

獲得 ip 地址后,您需要找到位置

# pip install geocoder

import geocoder

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]
        ip_location = geocoder.ip(f"{ip}")
        ip_location = geocoder.ip("me")
        print(ip_location.city)
        # you can get city such as "New York"
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip

使用這個 function 獲取 ip 地址:

def get_ip_address(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

之后,您可以從 web 應用程序http://www.iplocinfo.com/獲取用戶位置數據和其他信息:

import requests
def get_ip_data(request):
    ip_address = get_ip_address(request)
    api_key = "your api key"
    endPoint = f'https://www.iplocinfo.com/api/v1/{ip_address}?apiKey={api_key}'
    data = requests.get(endPoint)
    return data.json()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM