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

WBOY
Release: 2016-06-10 15:18:49
Original
1261 people have browsed it

通过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'
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!