如何使用python bottle框架获取客户端IP地址 [英] How to get client IP address using python bottle framework

查看:340
本文介绍了如何使用python bottle框架获取客户端IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用python的客户端IP地址.我试过下面的代码,但在服务器中不起作用:

from socket import gethostname, gethostbyname 
ip = gethostbyname(gethostname()) 
print ip

在服务器上,每次都会得到"127.0.0.1".有什么办法可以找到客户端的IP地址吗?

解决方案

您获得的是服务器的IP地址,而不是服务器的客户端的IP地址.

您要查看请求的 REMOTE_ADDR,如下所示:

from bottle import Bottle, request

app = Bottle()

@app.route('/hello')
def hello():
    client_ip = request.environ.get('REMOTE_ADDR')
    return ['Your IP is: {}\n'.format(client_ip)]

app.run(host='0.0.0.0', port=8080)


编辑#1 :有些人观察到,对于他们来说,REMOTE_ADDR的值始终是相同的IP地址(通常是127.0.0.1).这是因为它们位于代理(或负载平衡器)的后面.在这种情况下,客户端的原始IP地址通常存储在标头HTTP_X_FORWARDED_FOR中.无论哪种情况,下面的代码都可以工作:

@app.route('/hello')
def hello():
    client_ip = request.environ.get('HTTP_X_FORWARDED_FOR') or request.environ.get('REMOTE_ADDR')
    return ['Your IP is: {}\n'.format(client_ip)]


编辑#2 :由于@ArtOfWarfare的评论,我了解到PEP-333不需要REMOTE_ADDR.几个观察结果:

必须将REMOTE_ADDR变量设置为向服务器发送请求的客户端的网络地址.

  • 但是, PEP-333 并未明确要求HTTP_REMOTE_ADDR,只能达到此范围(强调我的观点):

服务器或网关应该尝试提供尽可能多的其他CGI变量.

  • 我熟悉集合HTTP_REMOTE_ADDR的所有(数量很少)Web框架. AFAICT,这是事实上的标准".但是从技术上讲,是YMMV.

I need client IP address using python. I have tried below code but its not working in server:

from socket import gethostname, gethostbyname 
ip = gethostbyname(gethostname()) 
print ip

On the server, I get '127.0.0.1' every time. Is there any way to find IP address of the client?

解决方案

You're getting the IP address of your server, not of your server's clients.

You want to look at the request's REMOTE_ADDR, like this:

from bottle import Bottle, request

app = Bottle()

@app.route('/hello')
def hello():
    client_ip = request.environ.get('REMOTE_ADDR')
    return ['Your IP is: {}\n'.format(client_ip)]

app.run(host='0.0.0.0', port=8080)


EDIT #1: Some folks have observed that, for them, the value of REMOTE_ADDR is always the same IP address (usually 127.0.0.1). This is because they're behind a proxy (or load balancer). In this case, the client's original IP address is typically stored in header HTTP_X_FORWARDED_FOR. The following code will work in either case:

@app.route('/hello')
def hello():
    client_ip = request.environ.get('HTTP_X_FORWARDED_FOR') or request.environ.get('REMOTE_ADDR')
    return ['Your IP is: {}\n'.format(client_ip)]


EDIT #2: Thanks to @ArtOfWarfare's comment, I learned that REMOTE_ADDR is not required per PEP-333. Couple of observations:

  • The CGI spec does require REMOTE_ADDR:

The REMOTE_ADDR variable MUST be set to the network address of the client sending the request to the server.

  • However, PEP-333 does not explicitly require HTTP_REMOTE_ADDR, only going as far as this (emphasis mine):

A server or gateway SHOULD attempt to provide as many other CGI variables as are applicable.

  • All of the (admittedly few) web frameworks that I'm familiar with set HTTP_REMOTE_ADDR. AFAICT, it's a de facto "standard." But technically, YMMV.

这篇关于如何使用python bottle框架获取客户端IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆