Python:如何判断一个http请求的源IP

Python: How can the origin IP of an http request be determined

我正在尝试做类似 ipify 的事情,一个简单的 public ip 地址 api。 一个http请求的ip地址如何用native来判断 python 3?

from werkzeug.wrappers import Request, Response


@Request.application
def application(request):
    return Response('Hello World!')

if __name__ == '__main__':
    from werkzeug.serving import run_simple
    run_simple('localhost', 4000, application)

我如何替换 return Response('Hello World!') 以使其 return 成为请求者的 ip?

好吧,我自己想出来了:

req_ip = request.remote_addr
return Response(req_ip)