开发者

Django: What's the correct way to get the requesting IP address?

开发者 https://www.devze.com 2022-12-30 15:20 出处:网络
I\'m trying to develop an app using Django 1.1 on Webfaction. I\'d like to get the IP address of the incoming request, but when I use request.META[\'REMOTE_ADDR\'] it returns 127.0.0.1. There seems to

I'm trying to develop an app using Django 1.1 on Webfaction. I'd like to get the IP address of the incoming request, but when I use request.META['REMOTE_ADDR'] it returns 127.0.0.1. There seems to be a number of different ways of getting the address, such as using HTTP_X_FORWARDED_FOR o开发者_运维问答r plugging in some middleware called SetRemoteAddrFromForwardedFor. Just wondering what the best approach was?


The remote proxy middleware was removed in Django 1.1.1 with a nod towards pointing out that trusting REMOTE_ADDR or HTTP_X_FORWARDED for isn't secure anyway (in case that also helps you decide what to do)


I use the middleware because this way I don't have to change the app's code.

If I want to migrate my app to other hosting servers, I only need to modify the middleware without affecting other parts.

Security is not an issue because on WebFaction you can trust what comes in from the front end server.

0

精彩评论

暂无评论...
验证码 换一张
取 消