Django 요청한 IP 주소 가져오기
11066 단어 python_web
HttpRequest.META A standard Python dictionary containing all available HTTP headers. Available headers depend on the client and server, but here are some examples: •CONTENT_LENGTH – The length of the request body (as a string). •CONTENT_TYPE – The MIME type of the request body. •HTTP_ACCEPT – Acceptable content types for the response. •HTTP_ACCEPT_ENCODING – Acceptable encodings for the response. •HTTP_ACCEPT_LANGUAGE – Acceptable languages for the response. •HTTP_HOST – The HTTP Host header sent by the client. •HTTP_REFERER – The referring page, if any. •HTTP_USER_AGENT – The client’s user-agent string. •QUERY_STRING – The query string, as a single (unparsed) string. •REMOTE_ADDR – The IP address of the client. •REMOTE_HOST – The hostname of the client. •REMOTE_USER – The user authenticated by the Web server, if any. •REQUEST_METHOD – A string such as “GET” or “POST”. •SERVER_NAME – The hostname of the server. •SERVER_PORT – The port of the server (as a string). With the exception of CONTENT_LENGTH and CONTENT_TYPE, as given above, any HTTP headers in the request are converted to META keys by converting all characters to uppercase, replacing any hyphens with underscores and adding an HTTP_ prefix to the name. So, for example, a header called X-Bender would be mapped to the META key HTTP_X_BENDER. Note that runserver strips all headers with underscores in the name, so you won’t see them in META. This prevents header-spoofing based on ambiguity between underscores and dashes both being normalizing to under- scores in WSGI environment variables. It matches the behavior of Web servers like Nginx and Apache 2.4+.
META 정보를 보려면 다음과 같이 하십시오.
request_meta = request.META
info = []
for k, v in request_meta.items():
info.append(k)
print(info)
['wsgi.version', 'RUN_MAIN', 'HTTP_REFERER', 'HTTP_HOST',
'SERVER_PROTOCOL', 'SERVER_SOFTWARE', 'SCRIPT_NAME',
'LESSOPEN', 'SSH_CLIENT', 'REQUEST_METHOD', 'LOGNAME',
'USER', 'HOME', 'QUERY_STRING', 'PATH', 'MYSQL_DATABASE_URI',
'wsgi.errors', 'TERADATA_JACKAL_URI', 'LANG', 'TERM',
'SHELL', 'TZ', 'HTTP_COOKIE', 'J2REDIR', 'REMOTE_ADDR',
'SHLVL', 'wsgi.url_scheme', 'HTTP_VIA', 'SERVER_PORT',
'wsgi.file_wrapper', 'JAVA_HOME', 'CONTENT_LENGTH',
'HTTP_CONNECTION', 'XDG_RUNTIME_DIR', 'TERADATA_PASSWORD',
'PYTHONPATH', 'COMP_WORDBREAKS', 'VIRTUAL_ENV', u'CSRF_COOKIE',
'J2SDKDIR', 'wsgi.input', 'HTTP_USER_AGENT', 'PS1',
'wsgi.multithread', 'HTTP_UPGRADE_INSECURE_REQUESTS',
'HTTP_CACHE_CONTROL', 'XDG_SESSION_ID', '_', 'HTTP_ACCEPT',
'DERBY_HOME', 'SSH_CONNECTION', 'LESSCLOSE', 'SERVER_NAME',
'GATEWAY_INTERFACE', 'HTTP_X_FORWARDED_FOR', 'SSH_TTY',
'OLDPWD', 'wsgi.multiprocess', 'HTTP_ACCEPT_LANGUAGE',
'wsgi.run_once', 'PWD', 'DJANGO_SETTINGS_MODULE',
'CONTENT_TYPE', 'TERADATA_SIMBA_URI', 'MAIL', 'LS_COLORS',
'REMOTE_HOST', 'HTTP_ACCEPT_ENCODING', 'PATH_INFO']
일반적으로 요청할 때마다 IP가 포함되어 있기 때문에 다음과 같은 방법으로 사용자의 실제 IP를 얻을 수 있습니다.
# X-Forwarded-For: XFF , , HTTP IP, HTTP 。
def get_ip(request):
''' IP '''
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') #
if x_forwarded_for:
ip = x_forwarded_for.split(',')[0] # ip
else:
ip = request.META.get('REMOTE_ADDR') # IP
return ip
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Django 요청한 IP 주소 가져오기HttpRequest.META A standard Python dictionary containing all available HTTP headers. Available headers depend on the cli...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.