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
# 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
Thank you!
0
0
3.83
5
# 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")
Thank you!
5
0
Are there any code examples left?
New code examples in category Python