1 def get_mac_address(): 2 mac=uuid.UUID(int = uuid.getnode()).hex[-12:] 3 return ":".join([mac[e:e+2] for e in range(0,11,2)])
获取计算机名字
1 import socket 2 socket.gethostname()
linux下获取主机外网ip
1 import socket 2 import fcntl 3 import struct 4 def get_ip_address(ifname): 5 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 6 return socket.inet_ntoa(fcntl.ioctl( 7 s.fileno(), 8 0x8915, # SIOCGIFADDR 9 struct.pack('256s', ifname[:15]) 10 )[20:24]) 11 print get_ip_address('lo') 12 print get_ip_address('eth0')
win linux兼容模式
1 import re,urllib2 2 class Getmyip: 3 def getip(self): 4 try: 5 myip = self.visit("http://www.ip138.com/ip2city.asp") 6 except: 7 try: 8 myip = self.visit("http://www.bliao.com/ip.phtml") 9 except: 10 try: 11 myip = self.visit("http://www.whereismyip.com/") 12 except: 13 myip = "So sorry!!!" 14 return myip 15 def visit(self,url): 16 opener = urllib2.urlopen(url) 17 if url == opener.geturl(): 18 str = opener.read() 19 return re.search('\d+\.\d+\.\d+\.\d+',str).group(0) 20 getmyip = Getmyip() 21 localip = getmyip.getip() 22 print localip
获取系统信息
Python里里面有个platform模块,可以得到操作系统的相关信息
import platform
platform.machine() # Returns the machine type, e.g. 'i386'
platform.node() # Returns the computer's network name
platform.platform(aliased=0, terse=0) # Returns a single string identifying the underlying platform
platform.processor() # Returns the (true) processor name
platform.python_branch() # Returns a string identifying the Python implementation branch.
platform.python_build() # Returns a tuple (buildno, builddate) stating the Python build number and date as strings.
platform.python_compiler() # Returns a string identifying the compiler used for compiling Python.
platform.python_version() # Returns the Python version as string 'major.minor.patchlevel'
platform.release() # Returns the system's release, e.g. '2.2.0' or 'NT'
platform.system() # Returns the system/OS name, e.g. 'Linux', 'Windows' or 'Java'.
platform.uname() # Fairly portable uname interface. Returns a tuple of strings (system,node,release,version,machine,processor) identifying the underlying platform.
platform.win32_ver(release='', version='', csd='', ptype='') # Get additional version information from the Windows Registry and return a tuple (version,csd,ptype) referring to version number, CSD level and OS type (multi/single processor).
linux系统下 python获取 内存、cpu、负载、网络流量、硬盘等信息
http://blog.csdn.net/huguangshanse00/article/details/17053789
http://blog.sina.com.cn/s/blog_62dc8af90102wr1j.html