DEV Community

loading...
Cover image for Find your global IP address using python

Find your global IP address using python

Sourav das
I am a computer science student and a full stack coder
・1 min read

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 :)

Discussion (0)