【问题标题】:How can I get the default machine domain name in Python using a portable way?如何使用可移植的方式获取 Python 中的默认机器域名?
【发布时间】:2016-01-19 14:15:46
【问题描述】:

我正在寻找一种用于获取当前机器默认域名的 Python 解决方案,该解决方案适用于 Linux、Mac OS X 和 Windows。

到目前为止,我发现:

  • Linux 有 dnsdomainname 返回它

【问题讨论】:

  • 你试过here的解决方案吗?
  • 也可以试试here

标签: python dns


【解决方案1】:

试试:

#!/usr/bin/env python

import socket

def get_dns_domain():
    return socket.getfqdn().split('.', 1)[1]

print (get_dns_domain()) # to match python>2.7

【讨论】:

    【解决方案2】:

    试试:

    from socket import gethostname
    machine_name=gethostname()
    print(machine_name)
    

    【讨论】:

      猜你喜欢
      • 2013-10-02
      • 2023-04-01
      • 2023-03-24
      • 2010-09-11
      • 1970-01-01
      • 1970-01-01
      • 2023-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多