Skip to main content

Track Phone Number Location Using Python

In our previous Python tutorial, we have explained how to develop Weather App in Python using Flask. In this tutorial, we will explain how to develop app to Track Phone Number Location Using Python.

We can easily get the phone number details in Python using packages. So here in this tutorial we will develop a web application to track Phone number location and related details using Python.

So let’s proceed with app development to Track Phone Number Location Using Python.

Application Setup

We will create application directory track-phone-number-python using below command.

$ mkdir track-phone-number-python

we moved to the project direcotry

$ cd track-phone-number-python

Modules Required

We will use folloing modules to implement to track phone number.

  • phonenumbers: This is Python package that can be used to get phone number details. You can install it using the below command:
  • pip install phonenumbers
    
  • Flask: As we will create a web application, so we will install Flask to create web applications using Python. You can install it using the below command::
  • pip install Flask
    

Create Application File

Now we will create our application file app.py at the root of project directory.

Then we will import installed packages flask and phonenumbers into our application file.

from flask import Flask, render_template, request, redirect, url_for
import phonenumbers
from phonenumbers import geocoder, carrier

We will create Flask application object and set route.

app = Flask(__name__)
     
@app.route('/')
@app.route('/trace', methods =['GET', 'POST'])

we will create function trace() and implement functionality to track phone number by getting form submitted post values. We will parse phone number using parse() function then pass that parse phone number to the function description_for_number() function to get number details. We will also get service provider details using name_for_number() function.

phoneNumber = phonenumbers.parse(number)
phoneDetails = geocoder.description_for_number(phoneNumber, 'en')
serviceProvider = carrier.name_for_number(phoneNumber, 'en')

After getting phone number details, we will pass details to template file trace.html to render values.

return render_template('trace.html', mesage = mesage, phoneNumber = phoneNumber, phoneDetails = phoneDetails, serviceProvider = serviceProvider)

Here is complete code from app.py file.

from flask import Flask, render_template, request, redirect, url_for
import phonenumbers
from phonenumbers import geocoder, carrier

app = Flask(__name__)
     
@app.route('/')
@app.route('/trace', methods =['GET', 'POST'])
def trace():
    mesage = ''    
    phoneNumber = ''
    phoneDetails = ''
    serviceProvider = ''
    if request.method == 'POST' and 'number' in request.form:
        number = request.form['number']
        if not number:
            mesage = 'Please enter mobile number with country code!'
        else:
            phoneNumber = phonenumbers.parse(number)
            phoneDetails = geocoder.description_for_number(phoneNumber, 
                                          'en')
            serviceProvider = carrier.name_for_number(phoneNumber,
            
                                  'en')
    return render_template('trace.html', mesage = mesage, phoneNumber = phoneNumber, phoneDetails = phoneDetails, serviceProvider = serviceProvider)
    
if __name__ == "__main__":
    app.run()                                      

Create Template File

We will create direcotry templates at root of project directory. Then we will create template file trace.html and create form html with an text input and a submit button.

<form action="{{ url_for('trace') }}" method="post">		
	<div class="form-group">
		<label for="email">Enter Phone Number(with country code):</label>
		<input type="text" class="form-control" id="number" name="number"  placeholder="+91**********" >
	</div>		
	<button type="submit" class="btn btn-primary">Trace</button>		
</form>

we will display phone number details after phone number entered and form submited.

{% if phoneDetails is defined and phoneDetails %}
	<div class="alert alert-success">Details: {{ phoneNumber }}</div>
	<div class="alert alert-success">Country : {{ phoneDetails }}</div>
	<div class="alert alert-success">Service Provider : {{ serviceProvider }}</div>
{% endif %}

Here is complete code from templates/trace.html file.

<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Trace Phone Number Location Using Python</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css">
</head>
<body>	
<div class="container">
	<br>
	<h2>Trace Phone Number Location Using Python</h2>
	<br>
	<form action="{{ url_for('trace') }}" method="post">
	    {% if mesage is defined and mesage %}
			<div class="alert alert-warning">{{ mesage }}</div>
		{% endif %}
		{% if phoneDetails is defined and phoneDetails %}
			<div class="alert alert-success">Details: {{ phoneNumber }}</div>
			<div class="alert alert-success">Country : {{ phoneDetails }}</div>
			<div class="alert alert-success">Service Provider : {{ serviceProvider }}</div>
		{% endif %}
		<div class="form-group">
			<label for="email">Enter Phone Number(with country code):</label>
			<input type="text" class="form-control" id="number" name="number"  placeholder="+91**********" >
		</div>		
		<button type="submit" class="btn btn-primary">Trace</button>		
	</form>
</div>
</body>
</html>