Hasan Mustafa
Hasan Mustafa

Reputation: 123

How to get and change Public IP in Python?

I have been trying to get the live IP address in python and change it using a proxy server. The code I have seen is for local IP not the Public IP.

How can I get public IP using python? i.e. the IP I get from whatismyip, and can change the public IP using a proxy request in Python on windows.

Upvotes: 1

Views: 4679

Answers (3)

new92
new92

Reputation: 26

You can also retrieve your public ip using ident:

import requests

response = requests.get('https://ident.me')
if response.status_code == 200:
    print(f"IP >>> {response.content.decode('utf8')}")

Upvotes: 1

greedalbadi
greedalbadi

Reputation: 1

  • you can get public ip address using hek.
import hek

# get ip 
ip = hek.ipstuff.myip(find="query")

# print ip
print(ip)

Upvotes: 0

Tarun Lalwani
Tarun Lalwani

Reputation: 146630

There are multiple services you can use to get your ip. The one I use mostly is ipinfo.io/ip

You can use below code

import requests

proxies = {
  'http': 'http://<IP>:<PORT>',
  'https': 'http://<IP>:<PORT>',
}

req = requests.get('http://ipinfo.io/ip', proxies=proxies)
print (req.text)

Upvotes: 2

Related Questions