问题:如何使用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.
回答 0
使用socket
及其gethostname()
功能。这将获得hostname
运行Python解释器的计算机的名称:
import socket
print(socket.gethostname())
Use socket
and its gethostname()
functionality. This will get the hostname
of the computer where the Python interpreter is running:
import socket
print(socket.gethostname())
回答 1
这两个都是可移植的:
import platform
platform.node()
import socket
socket.gethostname()
使用HOST
或HOSTNAME
环境变量的任何解决方案都是不可移植的。即使它在您的系统上运行时也可以运行,但是在cron等特殊环境中运行时也可能无法运行。
Both of these are pretty portable:
import platform
platform.node()
import socket
socket.gethostname()
Any solutions using the HOST
or HOSTNAME
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.
回答 2
无论如何,您可能都将加载os模块,因此另一个建议是:
import os
myhost = os.uname()[1]
You will probably load the os module anyway, so another suggestion would be:
import os
myhost = os.uname()[1]
回答 3
关于什么 :
import platform
h = platform.uname()[1]
其实你可能想看看所有的结果 platform.uname()
What about :
import platform
h = platform.uname()[1]
Actually you may want to have a look to all the result in platform.uname()
回答 4
os.getenv('HOSTNAME')
而且os.environ['HOSTNAME']
不总是工作。在cron作业和WSDL中,未设置HTTP HOSTNAME。使用此代替:
import socket
socket.gethostbyaddr(socket.gethostname())[0]
即使您在/ etc / hosts中定义了短别名,它也始终(即使在Windows上)返回标准主机名。
如果你定义的别名/ etc / hosts文件,然后socket.gethostname()
将返回别名。platform.uname()[1]
做同样的事情。
我遇到了以上方法不起作用的情况。这就是我现在正在使用的:
import socket
if socket.gethostname().find('.')>=0:
name=socket.gethostname()
else:
name=socket.gethostbyaddr(socket.gethostname())[0]
它首先调用gethostname来查看它是否返回类似于主机名的内容,如果不是,则使用我原来的解决方案。
os.getenv('HOSTNAME')
and os.environ['HOSTNAME']
don’t always work. In cron jobs and WSDL, HTTP HOSTNAME isn’t set. Use this instead:
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.
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:
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.
回答 5
如果我是正确的,那么您正在寻找socket.gethostname函数:
>> import socket
>> socket.gethostname()
'terminus'
If I’m correct, you’re looking for the socket.gethostname function:
>> import socket
>> socket.gethostname()
'terminus'
回答 6
From at least python >= 3.3:
You can use the field nodename
and avoid using array indexing:
os.uname().nodename
Although, even the documentation of os.uname suggests using socket.gethostname()
回答 7
socket.gethostname()
could do
回答 8
在某些系统上,主机名是在环境中设置的。如果是这种情况,则os模块可以通过os.getenv将其拉出环境。例如,如果HOSTNAME是包含所需内容的环境变量,则将通过以下方式获取它:
import os
system_name = 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:
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.
回答 9
我需要在PyLog conf文件中使用PC的名称,并且套接字库不可用,但是os库可用。
对于Windows,我使用了:
os.getenv('COMPUTERNAME', 'defaultValue')
其中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:
os.getenv('COMPUTERNAME', 'defaultValue')
Where defaultValue is a string to prevent None being returned
回答 10
您必须执行此行代码
sock_name = socket.gethostname()
然后,您可以使用该名称来查找addr:
print(socket.gethostbyname(sock_name))
You have to execute this line of code
sock_name = socket.gethostname()
And then you can use the name to find the addr :
print(socket.gethostbyname(sock_name))