python获取本机mac地址和ip地址列表的代码

把写内容过程中常用的一些内容段记录起来,如下内容段是关于python获取本机mac地址和ip地址列表的内容。

import sys, socket

def getipaddrs(hostname):
result = socket.getaddrinfo(hostname, None, 0, socket.SOCK_STREAM)
return [x[4][0] for x in result]

# the name of the local machine
hostname = socket.gethostname()

try:
print "IP addresses:", ", ".join(getipaddrs(hostname))
except socket.gaierror, e:
print "Couldn't not get IP addresses:", e






posted on 2019-07-29 09:50  cottonwood  阅读(398)  评论(0编辑  收藏  举报

导航