如何使用 Python 获取系统主机名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4271740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:00:50  来源:igfitidea点击:

How can I use Python to get the system hostname?

pythonhostname

提问by John

I'm writing a chat program for a local network. I would like be able to identify computers and get the user-set computer name with Python.

我正在为本地网络编写聊天程序。我希望能够识别计算机并使用 Python 获取用户设置的计算机名称。

采纳答案by Alex

Use socketand its gethostname()functionality. This will get the hostnameof the computer where the Python interpreter is running:

使用socket及其gethostname()功能。这将获得hostname运行 Python 解释器的计算机的 :

import socket
print(socket.gethostname())

回答by terminus

If I'm correct, you're looking for the socket.gethostname function:

如果我是对的,您正在寻找 socket.gethostname 函数:

>> import socket
>> socket.gethostname()
'terminus'

回答by vpit3833

socket.gethostname()could do

socket.gethostname()可以做

回答by GreenMatt

On some systems, the hostname is set in the environment. If that is the case for you, the os modulecan pull it out of the environment via os.getenv. For example, if HOSTNAME is the environment variable containing what you want, the following will get it:

在某些系统上,主机名是在环境中设置的。如果您是这种情况,os 模块可以通过os.getenv将其拉出环境。例如,如果 HOSTNAME 是包含您想要的环境变量,则以下内容将得到它:

import os
system_name = os.getenv('HOSTNAME')

Update: As noted in the comments, this doesn't always work, as not everyone's environment is set up this way. I believe that at the time I initially answered this I was using this solution as it was the first thing I'd found in a web search and it worked for me at the time. Due to the lack of portability I probably wouldn't use this now. However, I am leaving this answer for reference purposes. FWIW, it does eliminate the need for other imports if your environment has the system name and you are already importing the os module. Test it - if it doesn't work in all the environments in which you expect your program to operate, use one of the other solutions provided.

更新:如评论中所述,这并不总是有效,因为并非每个人的环境都是这样设置的。我相信在我最初回答这个问题时我正在使用这个解决方案,因为它是我在网络搜索中发现的第一件事,当时它对我有用。由于缺乏便携性,我现在可能不会使用它。但是,我将这个答案留作参考。FWIW,如果您的环境具有系统名称并且您已经在导入 os 模块,它确实消除了对其他导入的需要。测试它 - 如果它不能在您希望程序运行的所有环境中工作,请使用提供的其他解决方案之一。

回答by robert

Both of these are pretty portable:

这两个都非常便携:

import platform
platform.node()

import socket
socket.gethostname()

Any solutions using the HOSTor HOSTNAMEenvironment variables are not portable. Even if it works on your system when you run it, it may not work when run in special environments such as cron.

任何使用HOSTHOSTNAME环境变量的解决方案都是不可移植的。即使它在您运行时在您的系统上有效,但在特殊环境(例如 cron)中运行时可能无法正常工作。

回答by Lucien Hercaud

What about :

关于什么 :

import platform

h = platform.uname()[1]

Actually you may want to have a look to all the result in platform.uname()

其实你可能想看看所有的结果 platform.uname()

回答by Tom Ekberg

os.getenv('HOSTNAME')and os.environ['HOSTNAME']don't always work. In cron jobs and WSDL, HTTP HOSTNAME isn't set. Use this instead:

os.getenv('HOSTNAME')并且os.environ['HOSTNAME']并不总是有效。在 cron 作业和 WSDL 中,未设置 HTTP HOSTNAME。改用这个:

import socket
socket.gethostbyaddr(socket.gethostname())[0]

It always (even on Windows) returns a fully qualified host name, even if you defined a short alias in /etc/hosts.

它总是(即使在 Windows 上)返回一个完全限定的主机名,即使您在/etc/hosts 中定义了一个短别名。

If you defined an alias in /etc/hoststhen socket.gethostname()will return the alias. platform.uname()[1]does the same thing.

如果你定义的别名/ etc / hosts文件,然后socket.gethostname()将返回别名。platform.uname()[1]做同样的事情。

I ran into a case where the above didn't work. This is what I'm using now:

我遇到了上述方法不起作用的情况。这是我现在使用的:

import socket
if socket.gethostname().find('.')>=0:
    name=socket.gethostname()
else:
    name=socket.gethostbyaddr(socket.gethostname())[0]

It first calls gethostname to see if it returns something that looks like a host name, if not it uses my original solution.

它首先调用 gethostname 以查看它是否返回类似于主机名的内容,如果不是,则使用我的原始解决方案。

回答by mike0042

You will probably load the os module anyway, so another suggestion would be:

无论如何,您可能会加载 os 模块,因此另一个建议是:

import os
myhost = os.uname()[1]

回答by Bill Kidd

I needed the name of the PC to use in my PyLog conf file, and the socket library is not available, but os library is.

我需要在我的 PyLog conf 文件中使用 PC 的名称,并且套接字库不可用,但 os 库可用。

For WindowsI used:

对于我使用的Windows

os.getenv('COMPUTERNAME', 'defaultValue')

Where defaultValue is a string to prevent None being returned

其中 defaultValue 是一个字符串,以防止返回 None

回答by Shubham Chaudhary

From at least python >= 3.3:

至少从python >= 3.3 开始

You can use the field nodenameand avoid using array indexing:

您可以使用该字段nodename并避免使用数组索引:

os.uname().nodename

Although, even the documentation of os.unamesuggests using socket.gethostname()

虽然,即使是os.uname的文档建议使用socket.gethostname()