Python get an IP Address

In this python tutorial, you will learn what is IP Address and how to get the IP Address using the socket module in Python with examples. Here we will check:

  • Python get IP Address
  • Python get IP Address from hostname
  • Python get the IP Address of a website using a script
  • Python get an IP address from the URL
  • Determine if the given IP Address is public or private using the ipaddress module in python
  • Python IP Address validation
  • Python extract MAC address

What is an IP Address

  • IP stands for Internet Protocol Address. It is an address of your network hardware, where it connects your computer with other devices on your network and all over the world.
  • An IP Address is made up of characters or numbers like 508.543.12.514. All devices that are connected to an internet connection have a unique IP Address.

Python get IP Address

To get the IP address in Python of your computer we need to import the socket library, and then we can find the IP address of the computer, laptop, etc and they have their unique IP address.

Example:

import socket
h_name = socket.gethostname()
IP_addres = socket.gethostbyname(h_name)
print("Host Name is:" + h_name)
print("Computer IP Address is:" + IP_addres)
  • After writing the above code (python get the IP address), Ones you will print “IP_addres” then the output will appear as “ Host Name is: DESKTOP-AJNOCQ Computer IP Address is: 192.168.45.161 ”.
  • First, import the socket module and then get the h_name using the socket.gethostname().
  • Now, find the IP address by passing the h_name as an argument to the socket.gethostbyname() and store it in a variable. Print the IP address.

You can refer to the below screenshot for python get IP Address

Python get IP Address
Python get IP Address

Output:

Host Name is: DESKTOP-AJNOCQ
Computer IP Address is: 192.168.45.161

Python get IP Address from hostname

Python gethostbyname() function accept the hostname as an argument and it will return the IP address of some of the website by using the socket module.

Example:

import socket
IP_addres = socket.gethostbyname('pythonguides.com')
print("IP Address is:" + IP_addres)
  • After writing the above code (python get the IP address from hostname), Ones you will print “IP_addres” then the output will appear as “IP Address is: 104.28.20.90”.
  • Here, socket.gethostbyname() will return the IP address of the website.
  • If you are not in the same location as mine then you may get different IP addresses as output.

You can refer to the below screenshot for python get IP Address from from hostname

Python get IP Address from hostname
Python get IP Address from hostname

Python get the IP Address of a website using a script

Here, we will ask user to enter the website address and then print the IP address of that website.

Example:

import socket
host_name = input("Enter the website address: ")
print(f'The {host_name} IP address is: {socket.gethostbyname(host_name)}')
  • After writing the above code (python get the IP Address of a website using a script) first we will enter the website address, and then it will print the output as “ The food.com IP address is: 52.201.38.142”.
  • Here, socket.gethostbyname(host_name) will return the IP address of the website “food.com”.

You can refer to the below screenshot for python get IP Address of a website using a script.

Python get the IP Address of a website using a script
Python get the IP Address of a website using a script

Python get an IP address from the URL

Firstly, we have imported a socket module to get the IP address of a URL in python. URL stands for Uniform Resource Locator. A URL is an address of the resource on the internet.

Example:

import socket
url = "python.com"
print("IP Address:",socket.gethostbyname(url))
  • After writing the above code (python get an IP address from the URL) firstly, we will assign a URL to a variable.
  • The variable acts as an argument for the socket.gethostbyname(url) and it will return the output as “ IP address: 3.96.23.237”.

You can refer to the below screenshot for python get an IP address from the URL.

Python get an IP address from the URL
Python get an IP address from the URL

Determine if the given IP Address is public or private using the ipaddress module in python

  • Private IP address – A private IP address is the address of your device which is connected to the home or business network. This IP address cannot be accessed from devices outside your home or business network.
  • Public IP address – A public IP address is an address that is used to communicate outside the network. This IP address connects you to the world and it’s unique for all. A public IP address is assigned by the Internet Service Provider(ISP).

To determine whether the given IP Address is public or private we will first import ipaddress module, and we will use the is_private method of the ipaddress module, which will test the address is allocated for private.

Example:

from ipaddress import ip_address
def IP_address(IP: str)-> str:
    return "Private" if (ip_address(IP).is_private)else "Public"
if __name__ == '__main__':
    print(IP_address('127.0.0.1'))
    print(IP_address('3.96.23.237'))

After writing the above code (determine if the given IP Address is public or private using the ipaddress module in python) firstly, we will import the ipaddress module, and then we will use the is_private method of ipaddress. It will return the output as “Private Public”.

You can refer to the below screenshot for python get an IP address from the URL.

Determine if the given IP Address is public or private using the ipaddress module in python
Determine if the given IP Address is public or private using the ipaddress module in python

Python IP Address validation

If you want to check whether the given IP address is valid or not, use the socket module, and also we will use the function inet_aton() which will take only one argument.

Example:

import socket 
IP = '127.0.0.2561'
try:
   socket.inet_aton(IP)
   print("Valid IP address")
except socket.error:
   print("Invalid IP")

After writing the above code (python IP Address validation) ones you will print then the output will be “ Invalid IP address” because the given IP is not valid and hence the except block will be executed. If the given IP address is valid then it will return a valid IP address.

You can refer to the below screenshot for python IP Address validation.

Python IP Address validation
Python IP Address validation

Python extract MAC address

  • A media access control address(MAC address) is also known as a physical address is a unique identifier assigned to the network interface card(NIC) of the computer.
  • MAC address is a hardware identification number that uniquely identifies each device on a network.
  • NIC helps in connection of a computer with computers in the network.

To extract MAC address we will first import uuid module, and then we will use uuid.getnode() to extract MAC address of the computer.

Example:

import uuid
print(hex(uuid.getnode()))

After writing the above code (python extract MAC address) ones you will print “hex(uuid.getnode())” then the output will be “ 0x780cb8d4d2ca ”. But the visible output is not in format for and complex too.

You can refer to the below screenshot for python extract MAC address.

Python extract MAC address
Python extract MAC address

To get the MAC address in format form and with less complex way we will use getnode(), findall(), and re(). We need to import re and uuid module.

Example:

import re, uuid
print(" MAC address in less complex and formatted way is :", end="")
print(':'.join(re.findall('..', '%012x' %uuid.getnode())))

After writing the above code (python extract MAC address) ones you will print then the output will be “ MAC address in less complex and formatted way is: 78:0c:b8:d4:d2:ca”. Using join element of getnode() after each 2 digits using regex expression will provide in formatted way.

You can refer to the below screenshot for python extract MAC address.

Python extract MAC address
Python extract MAC address

You may like the following Python tutorials:

In this Python tutorial, we learned about Python get an IP Address. Also, We covered these below topics as:

  • What is an IP Address and how to get an IP address in Python
  • Python get IP Address from hostname
  • Python get the IP Address of a website using a script
  • Python get an IP address from the URL
  • Determine if the given IP Address is public or private using the ipaddress module in python
  • Python IP Address validation
  • Python extract MAC address