简体   繁体   中英

Get client ip with python

I'm a newbie in python. I want to write a simple web that prints the client ip on screen

my http.conf Handler: 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? 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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