Django获取请求的IP地址

在django官方文档中有一段对request.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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值