DEV Community

Cover image for Find your global IP address using python
Sourav das
Sourav das

Posted on

2 1

Find your global IP address using python

In this post i will show you how to find your global ip address using python , So why wait let the hacking begin !

First see the code:-

import bs4,requests

def ipadd(url):
    res = requests.get(url)
    res.raise_for_status()
    soup = bs4.BeautifulSoup(res.text,"html.parser")
    elems = soup.select("body > main:nth-child(1) > div:nth-child(1) > h1:nth-child(1)")
    return elems[0].text.strip()

ipis = ipadd("https://ipecho.net/")
print("Your global ip is "+ipis)
Enter fullscreen mode Exit fullscreen mode

Nothing difficult i am going to breakdown the code
first we are importing two modules bs4 aka beautifulsoup and requests
bs4 used for web scrapping .

  1. we are requesting to a url named 'ipecho.net'
  2. checking if any error has been raised or not
  3. scraping the request using bs4
  4. selecting that html part which has the info we need
  5. returning the ip in text or string format

It is simple isn't it :)

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay