如何使用Python获取系统主机名?
我正在为本地网络编写一个聊天程序。我希望能够使用Python识别计算机并获取用户设置的计算机名称。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
使用
socket
及其gethostname()
功能。这将获取运行 Python 解释器的计算机的主机名:Use
socket
and itsgethostname()
functionality. This will get thehostname
of the computer where the Python interpreter is running:这两者都非常可移植:
任何使用
HOST
或HOSTNAME
环境变量的解决方案都是不可移植的。即使它在您的系统上运行时可以运行,但在特殊环境(例如 cron)中运行时也可能无法运行。Both of these are pretty portable:
Any solutions using the
HOST
orHOSTNAME
environment 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.无论如何,您可能会加载 os 模块,因此另一个建议是:
You will probably load the os module anyway, so another suggestion would be:
怎么样:
实际上您可能想查看
platform.uname()
中的所有结果What about :
Actually you may want to have a look to all the result in
platform.uname()
os.getenv('HOSTNAME')
和os.environ['HOSTNAME']
并不总是有效。在 cron 作业和 WSDL 中,未设置 HTTP HOSTNAME。请使用它:它总是(即使在 Windows 上)返回完全限定的主机名,即使您在 /etc/hosts 中定义了一个短别名。
如果您在 /etc/hosts 中定义了别名,则
socket.gethostname()
将返回该别名。platform.uname()[1]
做同样的事情。我遇到过上述方法不起作用的情况。这就是我现在使用的:
它首先调用 gethostname 来查看它是否返回看起来像主机名的内容,如果没有,它使用我原来的解决方案。
os.getenv('HOSTNAME')
andos.environ['HOSTNAME']
don't always work. In cron jobs and WSDL, HTTP HOSTNAME isn't set. Use this instead:It always (even on Windows) returns a fully qualified host name, even if you defined a short alias in /etc/hosts.
If you defined an alias in /etc/hosts then
socket.gethostname()
will return the alias.platform.uname()[1]
does the same thing.I ran into a case where the above didn't work. This is what I'm using now:
It first calls gethostname to see if it returns something that looks like a host name, if not it uses my original solution.
至少从 python >= 3.3 开始:
您可以使用字段
nodename
并避免使用数组索引:尽管如此,即使是 os.uname 建议使用
socket.gethostname()
From at least python >= 3.3:
You can use the field
nodename
and avoid using array indexing:Although, even the documentation of os.uname suggests using
socket.gethostname()
如果我是正确的,您正在寻找 socket.gethostname 函数:
If I'm correct, you're looking for the socket.gethostname function:
你必须执行这行代码
然后你可以使用名称来查找 addr :
You have to execute this line of code
And then you can use the name to find the addr :
socket.gethostname()
可以socket.gethostname()
could do要获取完全限定的主机名,请使用 socket.getfqdn()
To get fully qualified hostname use socket.getfqdn()
在某些系统上,主机名是在环境中设置的。如果您是这种情况,os 模块 可以通过以下方式将其从环境中拉出: os.getenv。例如,如果 HOSTNAME 是包含您想要的环境变量,则以下内容将得到它:
更新:正如注释中所述,这并不总是有效,因为并非每个人的环境都是这样设置的。我相信,在我最初回答这个问题时,我正在使用这个解决方案,因为它是我在网络搜索中找到的第一个解决方案,并且当时它对我有用。由于缺乏可移植性,我现在可能不会使用它。不过,我留下这个答案仅供参考。 FWIW,如果您的环境具有系统名称并且您已经导入了 os 模块,那么它确实消除了其他导入的需要。测试它 - 如果它不能在您希望程序运行的所有环境中工作,请使用提供的其他解决方案之一。
On some systems, the hostname is set in the environment. If that is the case for you, the os module can 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:
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.
我需要在 PyLog conf 文件中使用 PC 的名称,并且套接字库不可用,但操作系统库可用。
对于 Windows 我使用:
其中 defaultValue 是一个字符串,以防止返回 None
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.
For Windows I used:
Where defaultValue is a string to prevent None being returned