Python获取Windows或Linux主机名称通用函数分享
作者:junjie 时间:2023-11-29 06:09:25
通过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,主机名称
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python中的print()输出
2023-06-27 18:07:03
交互设计实用指南系列(4)—“有效性”之“简洁清晰,自然易懂”
2010-01-05 16:52:00
![](https://img.aspxhome.com/file/UploadPic/up/2010010517023462.gif)
ORACLE实例的后台进程
2024-01-12 17:58:34
Git标签管理
2022-02-01 18:53:06
![](https://img.aspxhome.com/file/2023/5/120515_0s.png)
优雅地使用loading(推荐)
2024-04-30 08:42:01
![](https://img.aspxhome.com/file/2023/9/132999_0s.jpg)
实例:arguments.callee的应用
2009-04-20 12:38:00
如何用MySQL-Front远程连接MySql?
2010-12-03 16:02:00
python字典遍历数据的具体做法
2022-04-19 16:45:33
mysql 5.7.30安装配置方法图文教程
2024-01-28 02:18:33
![](https://img.aspxhome.com/file/2023/8/88488_0s.jpg)
一起来学习Python的列表
2022-08-04 06:06:23
javascript onmouseout 解决办法
2024-02-26 00:32:44
CSS+asp仿迅雷人气指数
2009-08-03 14:11:00
![](https://img.aspxhome.com/file/UploadPic/20098/3/520098210240-50s.jpg)
Apache SkyWalking 监控 MySQL Server 实战解析
2024-01-24 01:31:54
![](https://img.aspxhome.com/file/2023/0/115360_0s.png)
Tensorflow简单验证码识别应用
2023-08-10 14:13:14
![](https://img.aspxhome.com/file/2023/6/64646_0s.jpg)
python中matplotlib的颜色以及形状实例详解
2021-06-30 23:04:35
![](https://img.aspxhome.com/file/2023/2/105592_0s.png)
python GUI库图形界面开发之PyQt5切换按钮控件QPushButton详细使用方法与实例
2021-06-13 09:13:14
![](https://img.aspxhome.com/file/2023/0/76840_0s.png)
python中的mock接口开发示例详解
2023-03-05 00:53:29
wxPython之解决闪烁的问题
2022-05-12 13:21:30
![](https://img.aspxhome.com/file/2023/1/86361_0s.jpg)
flask-script模块的具体使用
2022-04-08 10:53:34
PowerBI和Python关于数据分析的对比
2023-07-08 15:52:45
![](https://img.aspxhome.com/file/2023/8/117988_0s.gif)