简体   繁体   中英

How can I return system information in Python?

Using Python, how can information such as CPU usage, memory usage (free, used, etc), process count, etc be returned in a generic manner so that the same code can be run on Linux, Windows, BSD, etc?

Alternatively, how could this information be returned on all the above systems with the code specific to that OS being run only if that OS is indeed the operating environment?

Regarding cross-platform: your best bet is probably to write platform-specific code, and then import it conditionally. eg

import sys
if sys.platform == 'win32':
  import win32_sysinfo as sysinfo
elif sys.platform == 'darwin':
  import mac_sysinfo as sysinfo
elif 'linux' in sys.platform:
  import linux_sysinfo as sysinfo
#etc

print 'Memory available:', sysinfo.memory_available()

For specific resources, as Anthony points out you can access /proc under linux. For Windows, you could have a poke around at the Microsoft Script Repository . I'm not sure where to get that kind of information on Macs, but I can think of a great website where you could ask :-)

In a Linux environment you could read from the /proc file system.

~$ cat /proc/meminfo
MemTotal:      2076816 kB
MemFree:        130284 kB
Buffers:        192664 kB
Cached:        1482760 kB
SwapCached:          0 kB
Active:         206584 kB
Inactive:      1528608 kB
HighTotal:     1179484 kB
HighFree:       120768 kB
LowTotal:       897332 kB
LowFree:          9516 kB
SwapTotal:     2650684 kB
SwapFree:      2650632 kB
Dirty:              64 kB
Writeback:          12 kB
AnonPages:       59668 kB
Mapped:          22008 kB
Slab:           200744 kB
PageTables:       1220 kB
NFS_Unstable:        0 kB
Bounce:              0 kB
CommitLimit:   3689092 kB
Committed_AS:   263892 kB
VmallocTotal:   114680 kB
VmallocUsed:      3604 kB
VmallocChunk:   110752 kB

psutil should provide what you need:

[...] cross-platform library for retrieving information on running processes and system utilization (CPU, memory, disks, network) [...]

[...] supports Linux, Windows, OSX, FreeBSD and Sun Solaris, both 32-bit and 64-bit architectures [...]

It looks like you want to get a lot more information than the standard Python library offers. If I were you, I would download the source code for 'ps' or 'top', or the Gnome/KDE version of the same, or any number of system monitoring/graphing programs which are more likely to have all the necessary Unix cross platform bits, see what they do, and then make the necessary native calls with ctypes.

It's trivial to detect the platform. For example with ctypes you might try to load libc.so, if that throws an exception try to load 'msvcrt.dll' and so on. Not to mention simply checking the operating system's name with os.name. Then just delegate calls to your new cross-platform API to the appropriate platform-specific (sorry) implementation.

When you're done, don't forget to upload the resulting package to pypi.

看看os 模块

There's the PSI (Python System Information) project with that aim, but they don't cover Windows yet.

You can probably use PSI and recpies like this one and create a basic library that meets your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM