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.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
리버스 프록시 (있는 경우)가 올바르게 구성되어 있는지 확인하십시오 (예 : mod_rpaf
Apache 용으로 설치).
참고 : 위의의 첫 번째 항목 X-Forwarded-For
을 사용하지만 마지막 항목 을 사용할 수 있습니다 (예 : Heroku의 경우 : Heroku에서 클라이언트의 실제 IP 주소 가져 오기 )
그런 다음 요청을 인수로 전달하십시오.
get_client_ip(request)
Python 2 & 3 을 지원 하고 IPv4 & IPv6을 처리 하는 django-ipware 를 사용할 수 있습니다 .
설치:
pip install django-ipware
간단한 사용법 :
클라이언트의 IP 주소를 얻으려면
# 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
yanchenko의 답변에 개선을 제안하고 싶습니다.
X_FORWARDED_FOR 목록에서 첫 번째 IP를 가져 오는 대신 일부 라우터가 프로토콜을 고려하지 않으므로 내부 IP를 알 수없는 첫 번째 IP를 가져 와서 내부 IP를 목록의 첫 번째 값으로 볼 수 있습니다.
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
이것이 동일한 문제가있는 동료 Google 직원에게 도움이되기를 바랍니다.
이 작업을 수행하는 짧은 하나의 라이너가 있습니다.
request.META.get('HTTP_X_FORWARDED_FOR', request.META.get('REMOTE_ADDR', '')).split(',')[0].strip()
가장 간단한 해결책 (fastcgi + nignx를 사용하는 경우)은 itgorilla가 다음과 같이 언급 한 것입니다.
이 위대한 질문에 감사드립니다. 내 fastcgi가 REMOTE_ADDR 메타 키를 전달하지 못했습니다. nginx.conf에 아래 줄을 추가하고 문제를 해결했습니다. fastcgi_param REMOTE_ADDR $ remote_addr; –이고 릴라
추신 : 나는 그의 해결책을 더 잘 보이게하기 위해이 대답을 추가했습니다.
내 경우에는 위의 작업 중 어느 것도 작동하지 않으므로 uwsgi
+ django
소스 코드 를 확인 하고 nginx에서 정적 매개 변수를 전달하고 왜 / 어떻게하는지, 아래에서 내가 찾은 것을 볼 필요가 있습니다.
환경 정보 :
Python 버전 : 2.7.5
Django (1, 6, 6, 'final', 0)
버전 : nginx/1.6.0
Nginx 버전 : uwsgi :2.0.7
환경 설정 정보 :
포트 80
uwsgi 에서 역방향 프록시 수신 으로 업스트림 유닉스 소켓으로 nginx , 결국 요청에 응답합니다
장고 구성 정보 :
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에서 정확히 동일한 필드 / 매개 변수 이름을 지정 request.META[field/param]
하고 django 앱에서 사용해야 합니다.
이제 미들웨어 (인터셉터)를 추가 할 것인지 아니면 HTTP_X_FORWARDED_FOR
특정 뷰에서 구문 분석 할 것인지 결정할 수 있습니다.
원래 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.com의 nginx는 다음으로 전송됩니다.
X-Forwarded-For: 8.8.8.8, 192.168.1.2, 192.168.1.3
은 X-Real-IP
당신이 맹목적으로 지침을 따르 경우 먼저 이전 프록시의 IP 될 것입니다.
경우 누가 사용자는 것은 문제가 신뢰, 당신은 같은 것을 시도 할 수 django-xff
: https://pypi.python.org/pypi/django-xff/를
위의 답변에서 프록시가 누락되었습니다. django_easy_timezonesget_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'"속성이 없습니다)
참고 URL : https://stackoverflow.com/questions/4581789/how-do-i-get-user-ip-address-in-django
'IT' 카테고리의 다른 글
의사 요소의 누적 순서를 부모 요소 아래에 설정할 수 있습니까? (0) | 2020.03.29 |
---|---|
문자열에서 단어 바꾸기-Ruby (0) | 2020.03.29 |
구문 오류 : 함수가 '£'를 반환 할 때 파일에 ASCII가 아닌 문자 '\ xa3' (0) | 2020.03.29 |
#if RELEASE가 ## DEBUG가 C #에서하는 것처럼 작동합니까? (0) | 2020.03.29 |
이클립스 댓글 / 댓글 바로 가기? (0) | 2020.03.29 |