使用 web.py 如何在别人访问我的页面时获取他们的 IP

Using web.py how can I get someone's IP when they visit my page

我找到了 this question,这似乎正是我要找的东西。

它说使用 web.ctx['ip']

但我无法让它正常工作,我只想要变量中访问者的 IP。到目前为止,我一直在尝试这个:

import web

urls = (
    '/', 'index'
)

class index:
    def GET(self):
        return "Hello, world!"
        return web.ctx['ip'] #Trying to get it to just *show* so
                             #I can put it into a variable

        print web.ctx['ip'] #Nothing happens here either

if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()

提前致谢,我想我已经很接近了,我只需要一点帮助。也许我需要导入其他东西?

您甚至在获得 IP 地址之前就返回了,只需按以下方式重新排序您的代码并删除 return "Hello, world!" :

class index:
    def GET(self):
        print web.ctx['ip'] 
        return web.ctx['ip']