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

本文是小编为大家收集整理的关于如何使用python bottle框架获取客户的IP地址的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我需要使用Python的客户端IP地址.我尝试了以下代码,但它在服务器中不起作用:

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

在服务器上,我每次都会获得'127.0.0.1'.有什么方法可以找到客户端的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的评论,我了解到REMOTE_ADDR REMOTE_ADDR每个PEP-333不需要.几个观察:

必须将远程_ADDR变量设置为客户端的网络地址,将请求发送到服务器.

  • 然而, pep-333 需要HTTP_REMOTE_ADDR,只能到达这个(强调我的):

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

  • 我熟悉set HTTP_REMOTE_ADDR的所有(肯定是很少的)Web框架. afaict,这是事实上的"标准".但是从技术上讲,YMMV.

其他推荐答案

服务器可能在代理后面.将其用于代理和正向支持:

request.environ.get('HTTP_X_FORWARDED_FOR') or request.environ.get('REMOTE_ADDR')

其他推荐答案

如果您要获得外部IP,则需要从外部来源获得它,即whatemyip.com或提供API的地方.如果这是您要寻找的,请查看请求模块 http://docs.pys.python-requests. org/

本文地址:https://www.itbaoku.cn/post/1975353.html

问题描述

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.

其他推荐答案

Server might be behind a proxy. Use this for proxy and forward support:

request.environ.get('HTTP_X_FORWARDED_FOR') or request.environ.get('REMOTE_ADDR')

其他推荐答案

If you're trying to get the external IP, you will need to get it from an external source, i.e whatismyip.com or somewhere that offers an api. If that's what you're looking for, take a look at the Requests module http://docs.python-requests.org/