1import socket
2hostname = socket.gethostname()
3IPAddr = socket.gethostbyname(hostname)
4print("Your Computer Name is:" + hostname)
5print("Your Computer IP Address is:" + IPAddr)
6#How to get the IP address of a client using socket
1import socket
2host = socket.getfqdn()
3addr = socket.gethostbyname(host)
4print(f"Your ip is {addr}")
5# On Linux, it may give you the localhost address
1import socket
2def get_ip():
3 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
4 try:
5 # doesn't even have to be reachable
6 s.connect(('10.255.255.255', 1))
7 IP = s.getsockname()[0]
8 except Exception:
9 IP = '127.0.0.1'
10 finally:
11 s.close()
12 return IP