In this post, we will discuss how we can check our machine’s external IP address using python?

The external IP address is the address assigned to our machine by our ISP (Internet Service Provider) which the internet or all the machines outside of our local network use it to identify us.

If we try to find the IP address of our machine using any kind of program that runs on our local machine then we will end up getting our local address instead of the external address.

Hence to get the external IP address assigned to our machine by the ISP we need to use an external source.

Now you know that we need an external source to check our external IP address, so let’s see how can we do that using python code.

Method 1:

This example requires the requests library to be installed. So make sure you install it before importing it in your python program. You can learn more about requests library here

Tip: If you are connected to VPN then use the Amazon AWS endpoint ‘https://checkip.amazonaws.com’ to get your external IP address.

Method 2:

Use the UPnP protocol to query your router for external IP address. Most importantly, this does not rely on an external service.

For this, we need to use an external library called miniupnp. So first install it in your python environment before using it in your program.

This method is unreliable when you are connected to some VPN, that’s why I initially told you that we need to use external service to get the proper result.

I hope everything is clear to you all. If you have any doubts or suggestions then please comment below.

Leave a Reply