Django 요청한 IP 주소 가져오기

11066 단어 python_web
django 공식 문서에 리퀘스트가 있습니다.META의 설명:
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

좋은 웹페이지 즐겨찾기