python get public ip address

from requests import get

ip = get('https://api.ipify.org').text
print(f'My public IP address is: {ip}')

0
0
Zhengyue 115 points

                                    # This example requires the requests library be installed.  You can learn more
# about the Requests library here: http://docs.python-requests.org/en/latest/

from requests import get

ip = get('https://api.ipify.org').text
print 'My public IP address is:', ip

0
0
3.83
5
Xin 95 points

                                    # this is more faster solution written in python2 tho :D
# comapre it with other solutions and you'll see

import urllib2

def get_public_ip(request_target):
	grabber = urllib2.build_opener()
	grabber.addheaders = [('Useragent','Mozilla/5.0')]
	try:
		public_ip_address = grabber.open(target_url).read()
	except urllib2.HTTPError, error:
		print("There was an error trying to get your Public IP: %s") % (error)
	except urllib2.URLError, error:
		print("There was an error trying to get your Public IP: %s") % (error)
	return public_ip_address

public_ip = "None"
target_url = "http://ip.42.pl/raw"
public_ip = get_public_ip(target_url)

if not "None" in public_ip:
	print("Your Public IP address is: %s") % (str(public_ip))
else:
	print("Your Public IP address was not found")

3.83 (12 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
python external ip how to get network's public IP using python py get client public ip get public ip adress python find ip public ip python get public ip in python python how to change external ip get public ip using python without requests get public ip using python check external ip and report python get external ip address using python get public ip from python "Python" get public IP address for all your instances Python get public IP address for all your instances get my public ip python python check public ip external ip python how to get your public ip with python linux python get public ip address public ip on python use python to get your public ip how to get local and public ip by pyhton get public ip of computer python how to get a public ip with python python router public ip python get public ip address of local machine get public ip python code get your public ip address in python how to find public ip address python python get public ip address outside vpn get your public ip from python how to get public ip address in python how to get the public ip address using python python get client public ip get external ip addess python print public ip adress python python command for displaying public IP Address how to get external ip address python python public ip python get global ip address python script to get public ip address how to get public ip in using pyhton get external ipof server in python python server public ip python get your external ip get your public ip python get external ip python python find external ip address how to get external ip addres in python how to get external ip with python python get public ip of host python what is my ip how to get the ip in python python get ip address how to get location from ip address in python find public ip python get external IP through python python get local ip python get ip python get ip info check ip python find ip with python python get ip address code python get local server ip ip public python making a ip public using python how to make a ip public using python getting public ip through python python get public ipv4 address in one line how to get public ip of client get request in python python os get public ip python get public ip address info get router ip python how to get public ip in python how to get public ip in pytohn public ip adress request python get public ip address python public ip adress python python get public ipv4 how to get public ip address using python get external ip in python python get my public ip python get external ip address print public ip from python python get my public ip address get my global ip python how to get the public ip in python how to get public IPv4 from a script python how to get public ip address with python how to find public ip with python how to check your public ip address in python get remote ip public python how to chek with python if my public ip is available python get the external ip,adress get public ip in python current machine getpublic ip in python what is my public ip address python how to get public ip python python get current public ip How to get public ip address python how to get exsternal ip in python get public ip with python get public ip with pythob python how to get public ip how to get user public ip address in python python code to get my publiuc python identify public ip address et public IP in python how to get my public ip python python how to get public ipv4 address get external ip address python python print external ip addess get my ipaddress on the internet python python get public ip address Python get external IP python get public ip python program to obtain public ip address get public ip python
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source