I just deployed a Flask app on Webfaction and I've noticed that request.remote_addr
is always 127.0.0.1
. which is of course isn't of much use.
How can I get the real IP address of the user in Flask on Webfaction?
Thanks!
I just deployed a Flask app on Webfaction and I've noticed that request.remote_addr
is always 127.0.0.1
. which is of course isn't of much use.
How can I get the real IP address of the user in Flask on Webfaction?
Thanks!
Flask の前にプロキシがある場合、次のようなものが Flask で実際の IP を取得します。
if request.headers.getlist("X-Forwarded-For"):
ip = request.headers.getlist("X-Forwarded-For")[0]
else:
ip = request.remote_addr
更新: Eli がコメントで言及した非常に良い点。これを単純に使用すると、セキュリティ上の問題が発生する可能性があります。詳細については、 Eli の投稿を参照してください。
問題は、おそらく Flask の前にある種のプロキシがあることです。この場合、「実際の」IP アドレスは多くの場合、request.headers['X-Forwarded-For']
.