CodeAs follows:
Import osimport sysimport subprocessdef get_linux_version (): Print ("System Version ---- % s" % ",". join (sys. version. split ("\ n") def get_cpu_info (): processor_cnt = 0 cpu_model = "" f_cpu_info = open ("/proc/cpuinfo") Try: For line in f_cpu_info: if (line. find ("processor") = 0): processor_cnt + = 1 Elif (line. find ("model name") = 0): If (cpu_model = ""): cpu_model = line. split (":") [1]. strip () print ("CPU counts: % s, CPU model: % s" % (processor_cnt, cpu_model) Finally: f_cpu_info.close () def get_mem_info (): mem_info = "" f_mem_info = open ("/proc/meminfo") Try: For line in f_mem_info: If (line. find ("memtotal") = 0): mem_info + = line. strip () + "," Elif (line. find ("swaptotal") = 0): mem_info + = line. strip () Break print ("mem_info ---- {: s }". format (mem_info) Finally: f_mem_info.close () def get_disc_info (): # disc_info = OS. popen ("DF-h "). read () # disc_info = subprocess. popen ("DF-h", shell = true ). communicate () [0] # print (disc_info) pipe = subprocess. popen ("DF-h", stdout = subprocess. pipe, shell = true) disc_info = pipe. stdout. read () print (disc_info)
without comments, the method is simple. The obtained version is the Sys object inside python. The obtained CPU and memory information is directly read the files under/proc, and then parse; to obtain hard disk information, run the shell command and save it to the python variable.