python get ip code example
Example 1: get external ip python
from requests import get
ip = get('https://api.ipify.org').text
print 'My public IP address is:', ip
Example 2: get IP address python
import socket
hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)
print("Your Computer Name is:" + hostname)
print("Your Computer IP Address is:" + IPAddr)
Example 3: how to get local ip in python
import socket
local_ip = socket.gethostbyname(socket.gethostname)
print(f"[+] Your Local IP is : {local_ip}")
Example 4: get ip python
import socket
host = socket.getfqdn()
addr = socket.gethostbyname(host)
print(f"Your ip is {addr}")
Example 5: how to get user ip in python
import socket
host_name = socket.gethostname()
IPAddress = socket.gethostbyname(host_name)
print("Your Computer Name is:" + host_name)
print("Your Computer IP Address is:" + IPAddress)
Example 6: python get lan ip
import socket
def get_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(('10.255.255.255', 1))
IP = s.getsockname()[0]
except Exception:
IP = '127.0.0.1'
finally:
s.close()
return IP