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

yipeiwu_com6年前Python基础

通过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'

相关文章

Python 常用string函数详解

字符串中字符大小写的变换 1. str.lower()   //小写 >>> 'SkatE'.lower() 'skate' 2. str.upp...

python 自动去除空行的实例

code 原文档 1.txt : Hello Nanjing 100 实现代码: file_ = "1.txt" r_file = open(file_, "r"...

Python 内置函数complex详解

英文文档: class complex([real[, imag]]) Return a complex number with the value real + imag*1j or...

Python tempfile模块学习笔记(临时文件)

tempfile.TemporaryFile 如何你的应用程序需要一个临时文件来存储数据,但不需要同其他程序共享,那么用TemporaryFile函数创建临时文件是最好的选择。其他的应用...

nginx搭建基于python的web环境的实现步骤

nginx搭建基于python的web环境的实现步骤

前言: 在搭建开始前,我们先来梳理下web服务工作流程,先看下图: 1、用户(PC)向web服务器发起http请求 2、web服务器判断用户请求文件是否为静态文件,是则直接读取静态文件...