码迷,mamicode.com
首页 > 编程语言 > 详细

python 获取本机环境信息

时间:2018-12-22 01:17:11      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:address   root   代码   try   pre   div   imp   drag   workgroup   

一、函数

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

 

python 获取本机环境信息

标签:address   root   代码   try   pre   div   imp   drag   workgroup   

原文地址:https://www.cnblogs.com/csms/p/10159433.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!