Home  >  Article  >  Backend Development  >  Python获取Windows或Linux主机名称通用函数分享

Python获取Windows或Linux主机名称通用函数分享

WBOY
WBOYOriginal
2016-06-10 15:18:491262browse

通过python的os模块获取windows或者linux主机名的通用函数。

复制代码 代码如下:

#!/usr/bin/env python 
#coding=utf-8 
 
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'
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn