Nginx+uWSGI+Django+SSL(https)安全证书中获取访问的IP地址信息

文章封面
# SECURE_PROXY_SSL_HEADER SSL安全证书中的
x_forwarded_for = request.META.get("SECURE_PROXY_SSL_HEADER")
    if x_forwarded_for:
        user_ip = x_forwarded_for.split(',')[-1].strip()
    else:
        user_ip = request.META.get('REMOTE_ADDR')

image 因为网站服务器会使用ngix等代理https(部署了SSL安全证书),如果没有部署SSL安全证书可以使用下面的代码获取http访问的IP地址

if request.META.has_key('HTTP_X_FORWARDED_FOR'):
    user_ip =  request.META['HTTP_X_FORWARDED_FOR']
else:
    user_ip = request.META['REMOTE_ADDR']

效果图: image

# 单纯测试可以不要参数url_ip,url_ip是访问网站地址目录
def blog_username_ip(request, url_ip):
    x_forwarded_for = request.META.get("SECURE_PROXY_SSL_HEADER")
    if x_forwarded_for:
        user_ip = x_forwarded_for.split(',')[-1].strip()
    else:
        user_ip = request.META.get('REMOTE_ADDR')
    # 获取用户名的名字
    username = request.META.get('USERNAME')
    # 获取用户访问的时间
    create_time = timezone.now()
    # 获取对应的数据,储存到MySQL数据库中
    # models.BlogUsernameIp.objects.create(username=username, ip=user_ip, ip_time=create_time, blog_url=url_ip)
    print("IP地址:" + user_ip, "用户名:" + username, "时间:" + str(create_time), "目录:" + url_ip)
    return None

个人博客:https://www.chenzhanxu.top/

评论

h
hw_7864

⭐️ Admin快速开发: https://show.cool-admin.com/

赞 0
h
hw_7864

⭐️ 官方文档: https://cool-js.com

赞 0
h
hw_7864

⭐️ chatgpt在线体验: https://cool-js.com/ai/chat-vip/index.html?r=helloworld

赞 0