我们有一个脚本使用平台模块来检测各种客户端的操作系统版本.
通过platform.py的源代码,我可以看到在Windows系统上,它使用的是sys.getwindowsverion().不幸的是,在Windows 8.1系统上,该特定功能报告:
>>> sys.getwindowsversion()
sys.getwindowsversion(major=6, minor=2, build=9200, platform=2, service_pack='')
Run Code Online (Sandbox Code Playgroud)
Windows 8.1是6.3.9600:
Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.
C:\Windows\system32>ver
Microsoft Windows [Version 6.3.9600]
Run Code Online (Sandbox Code Playgroud)
所以,我意识到我可以在调用platform.release()时编写一些额外的逻辑,如果返回8,则进行二次检查并尝试运行ver,但这似乎有点费解.
有谁知道更好的方法?
在重要的情况下运行ActivePython 2.7.2.5...
小智 4
Microsoft 改变了版本函数的行为方式。请参阅此页面了解更多信息。
我解决该问题的方法是使用 ctypes 和内核模式函数RtlGetVersion。尽管这是一个内核模式函数,但它可以从用户模式调用。我已经在许多版本的 Windows 上尝试过此操作,但没有遇到问题。
import ctypes
class OSVERSIONINFOEXW(ctypes.Structure):
_fields_ = [('dwOSVersionInfoSize', ctypes.c_ulong),
('dwMajorVersion', ctypes.c_ulong),
('dwMinorVersion', ctypes.c_ulong),
('dwBuildNumber', ctypes.c_ulong),
('dwPlatformId', ctypes.c_ulong),
('szCSDVersion', ctypes.c_wchar*128),
('wServicePackMajor', ctypes.c_ushort),
('wServicePackMinor', ctypes.c_ushort),
('wSuiteMask', ctypes.c_ushort),
('wProductType', ctypes.c_byte),
('wReserved', ctypes.c_byte)]
def get_os_version():
"""
Get's the OS major and minor versions. Returns a tuple of
(OS_MAJOR, OS_MINOR).
"""
os_version = OSVERSIONINFOEXW()
os_version.dwOSVersionInfoSize = ctypes.sizeof(os_version)
retcode = ctypes.windll.Ntdll.RtlGetVersion(ctypes.byref(os_version))
if retcode != 0:
raise Exception("Failed to get OS version")
return os_version.dwMajorVersion, os_version.dwMinorVersion
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2754 次 |
| 最近记录: |