How to redirect on Flask using IP2Location data

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 visitors 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:

Steps

  1. Open your Flask application for editing.
  2. Import the IP2Location Python library like this:import IP2Location
  3. Declare your database path and load the database like this:database = IP2Location.IP2Location(YOUR_DATABASE_PATH)
  4. 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 visitors 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': # Happened in Windows, if the IP got is 127.0.0.1, need to substitute with real IP address
            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)
  5. Next, you will need to create another function to display the translated page to the visitor. You can keep your translated texts in your template and then load it in the function. For example, if you have templates in English and Malay:
    @app.route('/home/<language>')
    def home(language=None):
        if language == 'en':
            return render_template(YOUR_EN_TEMPLATE_NAME, language=language)
        elif language == 'my':
            return render_template(YOUR_MY_TEMPLATE_NAME, language=language)
  6. Finally, save and push the changes to the production server.

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': # Happened in Windows, if the IP got is 127.0.0.1, need to substitute with real IP address
        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):
    if language == 'en':
        return render_template(YOUR_EN_TEMPLATE_NAME, language=language)
    elif language == 'my':
        return render_template(YOUR_MY_TEMPLATE_NAME, language=language)

 

Was this article helpful?

Related Articles