Introduction
Flask is a lightweight web framework written in Python which makes it easy to develop a web application. Developers have the freedom to choose the libraries or tools they want to use. In addition, developers can utilize third party libraries to identify visitors location. From there, developers can set various conditions to provide better user experience to visitors. For example, visitors can be redirected to the translated page if their native language is not English. In this article, we will show you how to redirect visitor in Flask using the IP2Location free LITE BIN database.
Installation
Before we start, you are required to download and install the following components in your server:
- IP2Location Python Library
- You can download and install from here: https://github.com/chrislim2888/IP2Location-Python
- IP2Location Database
- You can get the free IP2Location LITE databases from https://lite.ip2location.com
Steps
- Open your Flask application for editing.
- Import the IP2Location Python library like this:
import IP2Location
- Declare your database path and load the database like this:
database = IP2Location.IP2Location(YOUR_DATABASE_PATH)
- After that, you will need to create a function to listen on a specific path and catch the visitor’s IP address. Then the function will redirect visitor to the page in their respective languages. For example, if you wish to let the function to check on the home path, you can do like the following:
@app.route('/', methods=['GET']) def main(): global rec if request.environ.get('HTTP_X_FORWARDED_FOR') is None: ip = request.environ['REMOTE_ADDR'] else: ip = request.environ['HTTP_X_FORWARDED_FOR'] if ip == '127.0.0.1': # Only use if you are testing in localhost ip = REAL_IP_ADDRESS rec = database.get_all(ip) if rec.country_short == 'US' : return redirect(LINK_TO_ENGLISH_PAGE, code=302) elif rec.country_short == 'MY' : return redirect(LINK_TO_MALAY_PAGE, code=302)
- Next, you will need to create another function to display different language for different user. For example:
@app.route('/home/<language>') def home(language=None): return ('You will be served in ' + language.upper() + ' language.')
- Save the changes made in the previous step. After that, in your terminal, cd to the project directory and enter the following command to run the project:
flask --app <project_name> run
. Substitute the <project_name> with the name of the script that handle the routing. This will run the project at the http://127.0.0.1:5000/. Noted that this will only launches a very simple built-in server and is not suitable to deploy to production environment. To do so, you can refer to https://flask.palletsprojects.com/en/2.2.x/deploying/ to get started. - Now you can go to your browser, and navigate to the link http://127.0.0.1:5000/ to see the outcome. You will see a similar output displayed in the screenshot below.
Full code of Flask application is shown as below:
import IP2Location from flask import Flask, redirect, render_template, request app = Flask(__name__) database = IP2Location.IP2Location(YOUR_DATABASE_PATH) @app.route('/', methods=['GET']) def main(): global rec if request.environ.get('HTTP_X_FORWARDED_FOR') is None: ip = request.environ['REMOTE_ADDR'] else: ip = request.environ['HTTP_X_FORWARDED_FOR'] if ip == '127.0.0.1': # Only use if you are testing in localhost ip = REAL_IP_ADDRESS rec = database.get_all(ip) if rec.country_short == 'US' : return redirect(LINK_TO_ENGLISH_PAGE, code=302) elif rec.country_short == 'MY' : return redirect(LINK_TO_MALAY_PAGE, code=302) @app.route('/home/<language>') def home(language=None): return ('You will be served in ' + language.upper() + ' language.')