1classSetRemoteAddrFromForwardedFor(object):2"""
3 Middleware that sets REMOTE_ADDR based on HTTP_X_FORWARDED_FOR, if the
4 latter is set. This is useful if you're sitting behind a reverse proxy that
5 causes each request's REMOTE_ADDR to be set to 127.0.0.1.
6 Note that this does NOT validate HTTP_X_FORWARDED_FOR. If you're not behind
7 a reverse proxy that sets HTTP_X_FORWARDED_FOR automatically, do not use
8 this middleware. Anybody can spoof the value of HTTP_X_FORWARDED_FOR, and
9 because this sets REMOTE_ADDR based on HTTP_X_FORWARDED_FOR, that means
10 anybody can "fake" their IP address. Only use this when you can absolutely
11 trust the value of HTTP_X_FORWARDED_FOR.
12 """13defprocess_request(self, request):14try:15 real_ip = request.META['HTTP_X_FORWARDED_FOR']16except KeyError:17returnNone18else:19# HTTP_X_FORWARDED_FOR can be a comma-separated list of IPs. The20# client's IP will be the first one.21 real_ip = real_ip.split(",")[0].strip()22 request.META['REMOTE_ADDR']= real_ip
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。