• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Python gethostbyname() Function – Get IPv4 Address from Name

Python gethostbyname() Function – Get IPv4 Address from Name

May 8, 2022 Leave a Comment

The Python socket module gethostbyname() function allows us get the IPv4 address from a given name (computer, server, domain, etc.).

import socket

host_name = socket.gethostname()
IP_address_of_Computer = socket.gethostbyname(host_name)

IP_address_of_Google = socket.gethostbyname("google.com")

print(IP_address_of_computer)
print(IP_address_of_Google)

#Output:
10.0.0.220
172.217.4.46

When working with connections between different servers in Python, the ability to get the IP address of a client, computer or website can be very useful.

The Python socket module provides us a low-level networking interface.

One useful function from the socket module is the gethostbyname() function. gethostbyname() returns the IPv4 address given a host name.

With gethostbyname(), we can get your IP address or the IP address of any website.

Using gethostbyname() to Get the IP Address of a Computer Using Python

With the gethostbyname() function, you can get the public IP address of your computer.

To get the public IP address of my computer, first, we use the gethostname() function and then pass the host name to gethostbyname().

Below is an example of how you can get the public IP address of your computer with Python.

import socket

host_name = socket.gethostname()
IP_address = socket.gethostbyname(host_name)

print(IP_address)

#Output:
10.0.0.220

Using gethostbyname() to Get the IP Address of a Website Using Python

You can also use gethostbyname() to get the public IP address of a website.

To get the IP address of a website, you just pass the domain name of the website to gethostbyname().

Below is an example showing how to get the IP address of a website with Python.

import socket

IP_address = socket.gethostbyname("google.com")

print(IP_address)

#Output:
172.217.4.46

Hopefully this article has been useful for you to learn how to use gethostbyname() in Python.

Other Articles You'll Also Like:

  • 1.  Python Add Days to Date Using datetime timedelta() Function
  • 2.  Read Last N Lines of File in Python
  • 3.  How to Count Vowels in a String Using Python
  • 4.  How to Remove All Occurrences of a Character in a List Using Python
  • 5.  Check if String is Date in Python
  • 6.  Repeat String with * Operator in Python
  • 7.  Find All Pythagorean Triples in a Range using Python
  • 8.  pandas fillna – Replace NaN in Dataframe using Python
  • 9.  Add Minutes to Datetime Variable Using Python timedelta() Function
  • 10.  Using Python to Find Closest Value in List

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy