In this Python tutorial we are going to learn about IP Lookup Application with Flask, so for creating
IP Lookup Application with Flask we are going to use ipapi library.
Learn Django REST Framework for Beginner in Python
Also you can read Django articles in the below links
1: Django Pagination Complete Example
2: Django Sending Email to Gmail Account
3: Build News Application in Django
5: Django User Authentication Framework
6: Django IP Finder Application
Installation
First of all you need to install ipapi library, you can use pip for the installation.
1 |
pip install ipapi |
OK now we are going to use Flask library for this, Flask is one of the best and micro web
development framework in Python, make sure that you have already installed Flask if you
have not installed you can just use pip for the installation.
1 |
pip install flask |
Now let’s create our Project Structure in Flask, first of all you need to open your favorite IDE, in
this article iam using Pycharm IDE, create a New Project in Pycharm IDE, iam going to call it
FlaskIPFinder, after creating of the Project we need to create a Python file, iam going to call
it flaskipfinder.py, also you need to create a templates folder, in the templates folder create
two html files, the first one is base.html and the second one is index.html.
This is our flask Project Structure.
OK now open open your flaskipfinder.py file and you need to add this code in that file.
basically in the Index view function we are going to first get the input from the user,
right now we have not created our html code, after that we use our ipapi library for finding the
IP locations and information. also you need to send the information in your index.html.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import ipapi from flask import Flask, request, render_template #pip install ipapi app = Flask(__name__) @app.route('/', methods = ['GET', 'POST']) def Index(): search = request.form.get('search') data = ipapi.location(ip=search, output='json') return render_template('index.html', data=data) if __name__ == "__main__": app.run(debug=True) |
Now this is our base.html. also we have used Bootstrap CDN link in here.
templates/base.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"> <title>{% block title %} {% endblock %}</title> </head> <body> {% block body %} {% endblock %} </body> </html> |
And this is the index.html, basically we have a text input field for searching IP Address, also we
are rendering the data in our index.html file.
templates/index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
{% extends 'base.html' %} {% block title %} IP Finder {% endblock %} {% block body %} <div class="container"> <br> <br> <h1>Flask IP Finder - Codeloop.org</h1> <br> <br> <form action="{{url_for('Index')}}" method="post"> <div class="form-group"> <input type="text" name = "search" class="form-control" placeholder="Enter IP Address"> </div> <input type="submit" class="btn btn-primary" value="Search"> </form> <br> <br> <p>IP Address is : {{data.ip}}</p> <p>City Location is : {{data.city}}</p> <p>IP Country is : {{data.country}}</p> <p>Available Language is : {{data.languages}}</p> <p>Latitude Information Is : {{data.latitude}}</p> <p>Longitude Information Is : {{data.longitude}}</p> <p>Time Zone Information Is : {{data.timezone}}</p> <p>Availble Calling Code : {{data.country_calling_code}}</p> <p>Available Currency Is : {{data.currency}}</p> <p>Internet Network Is : {{data.org}}</p> <p>Continent Code Is : {{data.continent_code}}</p> <p>Postal Code Is : {{data.postal}}</p> <p>UTC Offset Is : {{data.utc_offset}}</p> </div> {% endblock %} |
So now run your Flask Project and go to http://localhost:5000/, and this is the result, first
it will show your IP Address, also you can search for an specific IP Address in Flask.
Also you can watch the complete video for this article
Subscribe and Get Free Video Courses & Articles in your Email
Comments are closed.