Python Django 搭建纯净IP地址返回服务(返回访问者
2020-03-27 本文已影响0人
拉灯的小手
环境:
- centos 7.5
- python 3.6.1
- nginx 1.16.1
- uwsgi 2.0.18
- django 2.2.7
==环境配置就不细说了,教程很多,也可看我之前写的一篇环境搭建流程==
https://blog.csdn.net/qq_38154948/article/details/103504154
Django获取客户端IP
# /Django_web/ip/ip.py
from django.http import HttpResponse
def get_ip(request):
if request.META.get('HTTP_X_FORWARDED_FOR'):
ip = request.META.get("HTTP_X_FORWARDED_FOR")
else:
ip = request.META.get("REMOTE_ADDR")
return HttpResponse(ip)
主url配置
# /Django_web/Djago_web/urls
from django.conf.urls import url
from ip import ip
urlpatterns = [
url(r'^$', ip.get_ip),
]
添加nginx配置文件 ip.conf
# /etc/nginx/conf.d/ip.conf
server{
listen 80; #监听80端口
server_name ip.域名.com;
charset utf-8;
access_log /var/log/nginx/access.log; #日志文件位置
error_log /var/log/nginx/error.log;
client_max_body_size 75M;
location / { #访问/时
include uwsgi_params; #加载uwsgi模块
uwsgi_pass 当前服务器IP地址:8000; #将连接转到该IP
uwsgi_read_timeout 2;
}
}
效果:
本文仅供学习交流使用,如侵立删!
企鹅 、WX: 1033383881