-4

I created a simple rest api via Flask.

@app.route('/login', methods=['POST'])
...

@app.errorhandler(404)
    def not_found(e):
        return render_template('404.html')

How I can tracking the request method, url, and IP address if the client access to wrong place (404 not found) in Flask. Thanks

BeoBeo
  • 21
  • 1
  • 8

1 Answers1

0

You can use request_method = request.method to get request method,request_ip_addr = request.remote_addr to get request ip address,request_url = request.url to get request url; I think you can code like this: @app.route('/login', methods=['POST']) ... @app.errorhandler(404) def not_found(e): request_method = request.method request_ip_addr = request.remote_addr request_url = request.url # then you can storage this parameters to database for tracking the error request method, url, and IP address. return render_template('404.html')

  • Thanks but the ip address not show client IP. It's always '127.0.0.1'. – BeoBeo Apr 13 '18 at 03:18
  • I think if you visit this page, if it's not from localhost, but from external network access, remote_addr will change. I think you can try to test flask servers and browsers on different computers. – Alex Sun Apr 13 '18 at 03:47
  • I'm running it in server with gunicorn and It still is 127.0.0.1. – BeoBeo Apr 13 '18 at 03:50