简体   繁体   English

用python获取客户端ip

[英]Get client ip with python

I'm a newbie in python. 我是python的新手。 I want to write a simple web that prints the client ip on screen 我想写一个简单的网页,在屏幕上打印客户端IP

my http.conf Handler: AddHandler mod_python .py PythonHandler mod_python.publisher PythonDebug On 我的http.conf处理程序:AddHandler mod_python .py PythonHandler mod_python.publisher PythonDebug On

The cgi.escape(os.environ["REMOTE_ADDR"]) return this error: KeyError: 'REMOTE_ADDR' and I just get lost with the BaseHTTPRequestHandler so what is the simple way to get the client ip? cgi.escape(os.environ [“REMOTE_ADDR”])返回此错误:KeyError:'REMOTE_ADDR'我只是迷失了BaseHTTPRequestHandler所以获取客户端ip的简单方法是什么? thank you. 谢谢。

In case you're concerned about scalability, this might be a bit faster: 如果您担心可伸缩性,这可能会更快一些:

from mod_python import apache
req.get_remote_host(apache.REMOTE_NOLOOKUP)

OK, I found the answer: 好的,我找到了答案:

from mod_python import apache
def client_ip(req):
    req.add_common_vars()
    return req.subprocess_env['REMOTE_ADDR']

It's working! 它的工作原理!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM