Find IP address and location using python

In this article we are going to learn about the various methods of getting a person’s ip address using python and also using this ip address to find the location of a person. Let us get started.

We are going to use the requests module in python to get all these data.

What is request module in python?

The requests module allows you to send HTTP requests like GET, POST, PUT and DELETE using Python. We are only going to use the get method to hit an url and get the response data

1. From “ipify.org”

The following url “https://api.ipify.org” returns the ip address of the user. This is how we execute the code in python.

ip address using python

But this url does not provide any further informations like address, state and country etc.

2. From “amazonaws.com”

The amazon aws also has a url called “https://checkip.amazonaws.com” which will also return only the ip to the user. This will work even when connected to a VPN.

ip address using python

3. From “ipinfo.io”

This is the best site to get all data other than our ip address. This site provides the following informations.

  1. ip address
  2. city
  3. country
  4. region
  5. postal
  6. timezone

The following code snippet will show you how to get the above informations from this site.

ip address using python

This code will return the following output.

 ip: "161.185.160.93"
 city: "New York City"
 region: "New York"
 country: "US"
 postal: "10004"
 timezone: "America/New_York"

They also have a python package but you may have to create an account to get the access keys. Hope this article was useful.

Happy coding!