python 获取本机环境信息

一、函数

1.socket.gethostname():不带任何参数,返回一个字符串(主机名),通常不完整。比如csm.example.com 只会返回csm

2.socket.getfqdn():带一个参数,返回完整主机名

 

二、代码

import sys,socket

def getipaddrs(hostname):
	"""Given a host name,perform a standard (forward) lookup and return a list of ip addressfor that host."""
	result=socket.getaddrinfo(hostname,None,0,socket.SOCK_STREAM)
	return [x[4][0] for x in result]

#calling gethostname() returns the name of the local machine
hostname=socket.gethostname()
print "hostname is:",hostname

#try to get the fully qualified name:
print "Fully_qualified name:",socket.getfqdn(hostname)
try:
	print "IP address:", ", ".join(getipaddrs(hostname))
except socket.gaierror,e:
	print "error"

三、执行结果

[root@csm testpython]# python host.py 
hostname is: csm
Fully_qualified name: dragoneyes-PC.workgroup
IP address: 192.168.155.9

 

转载于:https://www.cnblogs.com/csms/p/10159433.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值