• 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 / Get Public IP Address Using Python

Get Public IP Address Using Python

May 7, 2022 Leave a Comment

To get the public IP address of your computer, you can use the Python socket module gethostbyname() function.

import socket

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

print(IP_address)

#Output:
10.0.0.220

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

import socket

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

print(IP_address)

#Output:
172.217.4.46

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

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

With the socket module comes the gethostbyname() function which returns the IPv4 address given a host name.

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

How to Get the IP Address of a Computer Using Python

With the gethostbyname() function, we 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

How 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 Python to get the IP address of a computer or website.

Other Articles You'll Also Like:

  • 1.  Unset Environment Variables in Python
  • 2.  Python cosh – Find Hyperbolic Cosine of Number Using math.cosh()
  • 3.  Using Python to Get Day of Week
  • 4.  How to Remove All Punctuation from String in Python
  • 5.  How to Group By Columns and Find Maximum in pandas DataFrame
  • 6.  Using Python to Print Degree Symbol
  • 7.  Find Quotient and Remainder After Division in Python
  • 8.  Using Python to Remove Quotes from String
  • 9.  Read Last N Lines of File in Python
  • 10.  Python tanh – Find Hyperbolic Tangent of Number Using math.tanh()

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