WSGI 获取访问者 IP


WSGI 全称 Web Server Gateway Interface,基于 WSGI 的python 框架有 django,flask,tornado 等,我们可以很简单获取访问者 IP。

方案一:apache & mod_wsgi & python

创建 getip.wsgi,使用 apache 加载
getip.wsgi 文件内容如下:

#!/usr/bin/env python3

def application (environ, start_response):
  response_body = 'Your ip address is: %s\n' % environ['REMOTE_ADDR']
  status = '200 OK'
  response_headers = [('Content-Type', 'text/plain'),
                      ('Content-Length', '1'),
                      ('Content-Length', str(len(response_body)))]
  start_response(status, response_headers)
  return [response_body]

方案二:flask&python

创建 getip.py 文件,执行:(退出时使用exit,不要直接关闭终端)

nohup python3 getip.py &

getip.py 内容如下:

#!/usr/bin/env python3
from flask import Flask,request

app = Flask(__name__)

@app.route("/")

def get_ip():
  user_ip = request.remote_addr
  user_agent = request.user_agent
  return '<h5>This is a website by Python&Flask</h5> <b>Your ip address is</b> <h2>%s</h2> <br/> <b>Other information about you</b> <h3>%s</h3>' % (user_ip,user_agent)

if __name__ == '__main__':
  app.run(host='0.0.0.0',port=80)
python  wsgi