MyIP/findip.py

23 lines
575 B
Python
Raw Normal View History

2024-03-03 00:27:27 +01:00
#!/usr/bin/env python3
'''
/***********************************************/
/* Script para obtener mi IP interna y externa */
/***********************************************/
'''
from socket import gethostbyname, gethostname
from urllib.request import urlopen
import json
with urlopen("https://ipwho.is/?fields=ip") as response:
body = json.loads(response.read()) # {'ip': 'xxx.000.000.000'}
ip = body['ip']
#indice = ip.find('.')
#msb = ip[:indice]
#ip = ip.replace(msb, 'x' * len(msb))
print("IP interna:", gethostbyname(gethostname()))
print("IP externa:", ip)