Python 3 Script to Get Local and Public IP Address Using Socket and Requests Libraries
1K views
Jun 3, 2025
Buy the full source code of application here:
View Video Transcript
0:00
uh hello guys welcome to this video so
0:02
in this video I will show you a Python
0:04
script which will actually find out you
0:07
the local and public IP address of your
0:10
machine so every ISP have a unique IP IP
0:14
address attached to it so if you want to
0:17
find out your IP address then this
0:19
Python script will definitely uh just
0:22
help you in finding out your local and
0:24
public IP address so for this we will be
0:27
using the built-in packages of Python
0:30
which is the socket module and the
0:32
request module so these comes within the
0:35
Python installation so if I execute the
0:39
script now you will see it will tell me
0:41
the local IP address which is
0:45
192.168.1.2 and the public IP address
0:48
which is
0:51
103.1781.8 i can crossverify as well if
0:54
I go to a website find my IP
1:00
address
1:02
so you can see
1:05
that it is returning the same address
1:10
103.17081.8 you can see that this
1:15
also so in this way this is returning
1:18
the correct address and now I will show
1:22
you the script here
1:24
so first of all just you import the
1:28
necessary packages which is your socket
1:32
package and the request
1:34
package so we require the packages first
1:37
of all and then we
1:40
actually go to the main
1:51
function and inside the main function
1:53
right here we will actually
1:57
call
2:00
the the
2:02
functions which will be responsible to
2:05
define two functions the first function
2:08
will be returning the local IP address
2:10
the second one will be returning the
2:11
public IP address so now we need to
2:13
define these two
2:15
methods
2:17
so I will just define right here first
2:21
one is get local IP
2:26
and now for getting the local IP we
2:30
define one variable public IP and this
2:34
request basically will make a get
2:37
request to an API which is a free API
2:40
which will return you
2:42
the IP addresses so this is the API that
2:46
we are using right here
2:47
https
2:51
api.ipfi.org so if you hit this API it
2:54
will return you the public IP you can
2:56
see
2:57
that so this is the address here so we
3:00
running simply making a request to this
3:03
API for returning our public IP so once
3:09
we do text so this will return I will
3:13
simply return this public
3:16
IP like this and if any sort of now
3:21
error take place we can show the
3:25
error that's all so this completes the
3:28
function
3:31
and now the second function will be
3:34
responsible for getting
3:36
the which is your
3:39
uh this one for
3:42
get public IP and this one will be for
3:46
getting the local
3:53
IP and once again we will be doing this
3:58
inside for getting the local IP address
4:00
it's little bit different we basically
4:03
use the socket module and this contains
4:05
this function get host name we basically
4:09
get the host name after that using that
4:13
host name we will get the local IP
4:15
address so this socket contains this
4:18
function get host by name and we pass
4:22
the host
4:24
name like this and then this will be
4:27
returning the local IP so you can see
4:31
that for getting the local IP address we
4:34
are using the socket module these two
4:36
functions and for getting the public IP
4:39
we are making a simple get call to the
4:41
this API it's a free API you can call
4:44
make this get call and if I run this
4:46
script
4:48
now so it is
4:51
saying just the indentation
4:54
error so you can see now it is returning
4:58
your local IP and this is your public IP
5:01
so in this way inside Python you can get
5:04
these local and public IP addresses and
5:07
uh thank you very much for watching this
5:09
video and also check out my website
5:11
freemediatools.com
5:13
uh which contains thousands of tools
#Internet & Telecom
#Scripting Languages
#Web Services