利用python获取计算机主机名:用户设置的计算机名称

1、第一种方法

import socket

hostname = socket.gethostname()

print(hostname)

运行结果:

2、第二种方法

import platform

hostname = platform.node()

print(hostname)

运行结果:

3、第三种方法

import os

hostname = os.uname()[1]

print(hostname)

运行结果:

4、第四种方法

import os

hostname = os.popen('hostname').read()

print(hostname)

运行结果:

 

posted @   习久性成  阅读(1297)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
历史上的今天:
2021-12-02 urllib、urllib2、urllib3、request的详细区别
2021-12-02 python之selenium操作下拉滚动条方法 || selenium之页面滚动至页面底部的数种解决方案
2021-12-02 pytest执行之命令行参数--allure-no-capture:Do not attach pytest captured logging/stdout/stderr to Allure Report
2021-12-02 pytest之添加日志:pytest测试框架有自己的日志管理开关
点击右上角即可分享
微信分享提示