getfqdn() - Get the fully qualified domain name of a host using Python

The getfqdn() method of Python's socket module returns the fully qualified domain name of a computer on the internet. The example Python program uses getfqdn() method to get the FQDN of the local host and example.net.

socket Updated 21 Jun 2019 1 min read

Overview

  • The method **getfqdn()**of Python's socket module returns the fully qualified domain name of a host.
  • The Python example program invokes getfqdn()**twice. The first call to the **getfqdn()**is without passing any argument. In this case, it returns the fully qualified domain name of the machine name on which the Python interpreter is running. The return value is MyMachine.Local.The **Local is the pseudo-top-level domain for hostnames in local area networks.
  • The second invocation of getfqdn()**is done with “example.net” as the argument, which returns “example.net” as the output. The **.net part denotes the top-level domain (TLD). The name of the host is example.

Example

python
import socket

# Get the fully qualified domain name

fqdn = socket.getfqdn()

print("Fully qualified domain name of this computer is:");

print(fqdn);

# Get FQN for example.net

hostName = "www.example.net";

fqdn = socket.getfqdn(hostName);

print("Fully qualified domain name of %s is:"%hostName);

print(fqdn);

Output

Output
Fully qualified domain name of this computer is:

MyMachine.local

Fully qualified domain name of www.example.net is:

www.example.net