16 lines
400 B
Python
16 lines
400 B
Python
# To get the IPv4 IP address of the local machine using
|
|
# Using socket module and socket.socket() and connect()
|
|
# and socket.getsockname() Method in Python
|
|
|
|
# Import Module
|
|
import socket
|
|
|
|
# Create a socket object
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
# connect to the server on local computer
|
|
s.connect(("8.8.8.8", 80))
|
|
|
|
# Print Output
|
|
print("HostName: ",s.getsockname()[0])
|
|
s.close() |