Linux 上的 Python:在 /etc/hostname 中获取主机名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8263192/
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-06 02:33:45  来源:igfitidea点击:

Python on Linux: get host name in /etc/hostname

pythonlinuxip

提问by alexfernandez

From within a Python script I am trying to get the host name in a Linux box. It is a Debian GNU/Linux Amazon EC2 instance. I have set the correct name in /etc/hostname. The recommended solution socket.gethostname()is not working: it shows the ip- plus the IP tuple.

我试图从 Python 脚本中获取 Linux 机器中的主机名。它是一个 Debian GNU/Linux Amazon EC2 实例。我在/etc/hostname. 推荐的解决方案socket.gethostname()不起作用:它显示了 ip- 加上 IP 元组。

I have searched on StackOverflow and nothing is coming out, e.g. here. socket.getfqdn()is even worse: it yields ip-[IP tuple].eu-west-1.compute.internal.

我在 StackOverflow 上搜索过,但没有任何结果,例如这里socket.getfqdn()更糟糕的是:它产生ip-[IP tuple].eu-west-1.compute.internal.

Am I doing something wrong, or is there no clean solution to get the hostname in /etc/hostname? Of course the back-up solution is to read the file etc/hostnameitself, but something so inherently platform-dependent is somehow too aberrant. Thanks!

我做错了什么,还是没有干净的解决方案来获取主机名/etc/hostname?当然,备份解决方案是读取文件etc/hostname本身,但某些本质上依赖于平台的东西在某种程度上太不正常了。谢谢!

采纳答案by alexfernandez

First of all, all credit should go to m1k3y02, who gave me the hint in a comment. Now, for the sake of posterityI will give the correct answer: my Amazon EC2 has not been rebooted in a long time. I set the hostname in /etc/hostnamebut it did not reach the system, as evidenced by uname -n. So simply running /etc/init.d/hostname.shdid the trick. After that, socket.gethostname()worked as intended.

首先,所有功劳都归功于 m1k3y02,他在评论中给了我提示。现在,为了后代,我将给出正确答案:我的 Amazon EC2 已经很长时间没有重新启动了。我设置了主机名,/etc/hostname但它没有到达系统,正如uname -n. 所以简单地跑步/etc/init.d/hostname.sh就成功了。之后,socket.gethostname()按预期工作。

The lesson here: first see if the system gets it, and only after thatblame the language.

这里的教训:首先看看系统是否得到它,只有在此之后责备的语言。

回答by Emir Akayd?n

Have you tried socket.gethostbyaddr(socket.gethostname())?

你试过socket.gethostbyaddr(socket.gethostname())吗?

回答by Nate

Try os.uname(). According to the doc, it is the second position in the tuple returned.

试试os.uname()。根据doc,它是返回的元组中的第二个位置。

But, as the doc itself states, the "better way to get the hostname is socket.gethostname()or even socket.gethostbyaddr(socket.gethostname())."

但是,正如文档本身所说,“获取主机名的更好方法是socket.gethostname()甚至socket.gethostbyaddr(socket.gethostname()).”。

回答by phihag

The generic source for the hostname is hostname(1). This program calls the equivalent of uname -n.

主机名的通用来源是hostname(1)。这个程序调用相当于uname -n.

In Python, you can either use platform.node()or os.uname()[1].

在 Python 中,您可以使用platform.node()os.uname()[1]