乐闻世界logo
搜索文章和话题

How do I get my computer's fully qualified domain name in Python?

1个答案

1

In Python, obtaining the fully qualified domain name (FQDN) of a computer can be achieved using the socket module from the standard library. Here is a simple example:

python
import socket def get_fqdn(): # Retrieve the local hostname hostname = socket.gethostname() # Retrieve the fully qualified domain name fqdn = socket.getfqdn(hostname) return fqdn # Output the fully qualified domain name print(get_fqdn())

In this code snippet, the socket module is first imported, and a get_fqdn function is defined. Inside the function, the local hostname is retrieved using socket.gethostname(), and the hostname is converted to its fully qualified domain name using the socket.getfqdn() method. Finally, the function returns this FQDN.

This method is typically cross-platform and can run on various operating systems, including Windows, Linux, and macOS.

2024年8月16日 00:25 回复

你的答案