通过python的os模块获取windows或者linux主机名的通用函数。
复制代码 代码如下:
import os
def hostname():
sys = os.name
if sys == 'nt':
hostname = os.getenv('computername')
return hostname
elif sys == 'posix':
host = os.popen('echo $HOSTNAME')
try:
hostname = host.read()
return hostname
finally:
host.close()
else:
return 'Unkwon hostname'
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8