self_ip.py 634 B

12345678910111213141516171819
  1. from flask.ext.babel import gettext
  2. name = "Self IP"
  3. description = gettext('Display your source IP address if the query expression is "ip"')
  4. default_on = True
  5. # attach callback to the post search hook
  6. # request: flask request object
  7. # ctx: the whole local context of the pre search hook
  8. def post_search(request, ctx):
  9. if ctx['search'].query == 'ip':
  10. x_forwarded_for = request.headers.getlist("X-Forwarded-For")
  11. if x_forwarded_for:
  12. ip = x_forwarded_for[0]
  13. else:
  14. ip = request.remote_addr
  15. ctx['search'].answers.clear()
  16. ctx['search'].answers.add(ip)
  17. return True