• 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
  • Ruby
  • 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.  pandas Drop Rows – Delete Rows from DataFrame with drop()
  • 2.  Sort by Two Keys in Python
  • 3.  Python Replace Space With Dash Using String replace() Function
  • 4.  How to Count Vowels in a String Using Python
  • 5.  pandas cumprod – Find Cumulative Product of Series or DataFrame
  • 6.  Random Number Without Repeating in Python
  • 7.  How to Check if Number is Power of 2 in Python
  • 8.  How to Hide Turtle in Python with hideturtle() Function
  • 9.  Python Logging Timestamp – Print Current Time to Console
  • 10.  Check if Word is Palindrome Using Recursion with Python

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy