Home > Article > Backend Development > How Can I Get the IP Addresses of Visitors Using Flask in Python?
How to Acquire the IP Addresses of Visitors with Flask in Python
To facilitate logging operations, it is essential to obtain the IP addresses of users accessing your website. This is especially pertinent when such users are authorized to download files. In this article, we will guide you through the process of retrieving IP addresses using Flask, a popular Python-based micro-framework.
Getting the IP Addresses Through Flask
The documentation provided by Werkzeug, the WSGI toolkit upon which Flask is built, offers insights into accessing the Request object, which enables the retrieval of the remote_addr attribute.
Illustrative Code
The following code snippet demonstrates the process of retrieving IP addresses:
<code class="python">from flask import request from flask import jsonify @app.route("/get_my_ip", methods=["GET"]) def get_my_ip(): return jsonify({'ip': request.remote_addr}), 200</code>
Additional Information
For more thorough information, it is recommended to consult the official Werkzeug documentation.
The above is the detailed content of How Can I Get the IP Addresses of Visitors Using Flask in Python?. For more information, please follow other related articles on the PHP Chinese website!