Python 3 Script to Get IP Address Details Using IPinfo API and Save to CSV File
260 views
Jun 3, 2025
Get the full source code of application here: https://codingshiksha.com/python/python-3-script-to-get-ip-address-details-using-ipinfo-api-and-save-to-csv-file/
View Video Transcript
0:00
uh hello guys uh welcome to this video
0:02
so in this video I will show you how to
0:05
return the information about a specific
0:08
IP address inside your Python script so
0:12
this Python script will actually take an
0:15
IP
0:15
address and return the information about
0:18
that IP address you can track where this
0:21
IP address is located the country or
0:24
region everything so if I just
0:28
uh execute this script you will see the
0:32
IP details saved to so this CSV file is
0:36
generated and you can see we inputed
0:40
this IP address which is
0:43
172 and this details are submitted you
0:45
will see the region is Oakuckland New
0:48
Zealand you will see the location
0:51
latitude longitude everything is written
0:53
the full address time zone as
0:57
well so there is a package here inside
1:00
Python which returns all this
1:01
information for completely free we are
1:04
not using any sort of API key you can do
1:06
this unlimited number of times for
1:08
completely free so the name of the API
1:11
is IP
1:13
info this is a free API which doesn't
1:18
cost you money if you go to this if you
1:21
go to the pricing of this API you will
1:23
see unlimited request you can
1:26
make $0 per month so don't need to
1:29
provide any API key and uh you simply
1:34
hit this API and it will return you the
1:36
response so let me show you the script
1:40
the full script is given in the
1:41
description of this video so first of
1:44
all we need the request module and also
1:47
the CSV module as well then we
1:51
simply specify the IP
1:55
address which you want to get the
1:58
information so you input your IP
2:06
address and then you simply call this
2:09
function get IP info i will define this
2:12
and pass the IP address as an
2:15
argument so now we just need to define
2:18
this custom function which is get IP
2:21
info so inside this function we just
2:24
need to make a simple rest API call to
2:26
this API which is
2:29
HTTPS IP info.io
2:32
IO and here we specify our IP
2:40
address so dollar is not
2:44
necessary and
2:48
/json so this will return a JSON
2:50
response to us so we just just need to
2:52
ex parse it so inside the request module
2:56
we get this get method to send this API
2:59
request and now the response will be
3:01
coming so if the status code is 200 then
3:05
the request is successful then we simply
3:07
need to convert this response to JSON we
3:10
can do this and
3:12
then in the else case we can return none
3:17
that's all so it will return you the
3:19
response in a JSON
3:21
format and then we can simply save
3:27
this after we get this we can have a if
3:30
condition that if info is defined then
3:33
we need to save this to
3:37
CSV and then we can see IP information
3:40
has been saved so now we just need to
3:42
define this function save to CSV which
3:45
will actually carry out the process for
3:47
saving the data
3:52
so it will have the
3:54
actual data and the file
3:58
name then we simply use the open
4:00
function we simply open the file name in
4:04
the write
4:07
mode and then the new line will be
4:11
nothing
4:13
and encoding you can provide UTF8
4:17
and then uh for writing the CSV file we
4:20
simply use
4:23
the CSV writer class so CSV it contains
4:27
this function dictionary writer and here
4:30
we specify the file and the field names
4:33
which is data
4:36
keys and then we call this function
4:39
write
4:40
header and then writer dot write
4:44
row so this completes your script that's
4:47
all that is required for this
4:49
application let me delete this file once
4:52
again run the Python
4:54
script so you can see it
4:57
will return you the data here you can
5:00
take any IP address it will return you
5:02
the CSV file holding all this data for
5:06
us so let me verify my IP address so I
5:10
will go to what is my IP address
5:17
so as you can see this is my IP address
5:20
so I copy
5:22
this so you'll see this website is
5:25
returning all this
5:26
information so if I input
5:29
this IP
5:39
address so let me run this script now
5:42
for this IP
5:44
address so if you open this your CSV
5:47
file you will
5:49
see it is returning the correct address
5:51
you will see the latitude longitude the
5:55
ISP provider everything has been
5:57
successfully returned with the correct
5:59
information so in this way you can track
6:02
any IP address inside Python using this
6:05
uh API IP info please hit the like
6:08
button subscribe the channel and also
6:10
check out my website freemediattools.com
6:13
uh which contains thousands of tools
#Programming
#Scripting Languages
#Web Services