Extracting IP from request in Python

2019-05-10 11:30发布

问题:

I have a Pythonic HTTP server that is supposed to determine client's IP. How do I do that in Python? Is there any way to get the request headers and extract it from there?

PS: I'm using WebPy.

回答1:

Use web.ctx:

class example:
    def GET(self):
        print web.ctx.ip

More info here



回答2:

web.env.get('REMOTE_ADDR')