获取运行python脚本的Windows计算机的名称? [英] Getting name of windows computer running python script?

查看:48
本文介绍了获取运行python脚本的Windows计算机的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网络上有几台 Windows 计算机将运行 Python 脚本.脚本中应使用一组不同的配置选项,具体取决于运行此脚本的计算机.

如何在 python 脚本中获取该计算机名称?

假设脚本在名为 DARK-TOWER 的计算机上运行,​​我想编写如下内容:

<预><代码>>>>python.library.get_computer_name()'暗塔'

是否有我可以使用的标准或第三方库?

解决方案

原来有三个选项(包括前面已经回答的两个):

<预><代码>>>>进口平台>>>进口插座>>>导入操作系统>>>平台节点()'暗塔'>>>socket.gethostname()'暗塔'>>>os.environ['计算机名']'暗塔'

I have a couple Windows computers on my network that will be running a python script. A different set of configuration options should be used in the script depending on which computer is running this script.

How would I get that computer name in the python script?

Let's say the script was running on a computer named DARK-TOWER, I'd like to write something like this:

>>> python.library.get_computer_name()
'DARK-TOWER'

Is there a standard or third party library I can use?

解决方案

It turns out there are three options (including the two already answered earlier):

>>> import platform
>>> import socket
>>> import os
>>> platform.node()
'DARK-TOWER'
>>> socket.gethostname()
'DARK-TOWER'
>>> os.environ['COMPUTERNAME']
'DARK-TOWER'

这篇关于获取运行python脚本的Windows计算机的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆