Write a Python program to get the name of the host on which the routine is running

example 1:-

import socket
host_name = socket.gethostname()
print("Host name:", host_name)

output:-

Host name: LAPTOP-3KV6KVJ7

example 2:-
import socket
if socket.gethostname().find('.')>=0:
    name=socket.gethostname()
    print(name)
else:
    name=socket.gethostbyaddr(socket.gethostname())[0]
    print(name)
output:-
LAPTOP-3KV6KVJ7

Post a Comment

If you have any doubts, Please let me know
Thanks!

Previous Post Next Post