How can I return system information in Python?

后端 未结 7 1047
北荒
北荒 2020-12-01 02:44

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 L

相关标签:
7条回答
  • 2020-12-01 03:26

    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.

    0 讨论(0)
  • 2020-12-01 03:28

    I recommend the platform module:

    http://doc.astro-wise.org/platform.html

    http://docs.python.org/library/platform.html

    http://www.doughellmann.com/PyMOTW/platform/index.html

    0 讨论(0)
  • 2020-12-01 03:28

    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.

    0 讨论(0)
  • 2020-12-01 03:41

    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 [...]

    0 讨论(0)
  • 2020-12-01 03:47

    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
    
    0 讨论(0)
  • 2020-12-01 03:48

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

    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 :-)

    0 讨论(0)
提交回复
热议问题