最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python腳本獲取操作系統(tǒng)版本信息

 更新時(shí)間:2016年12月17日 09:11:06   作者:通信,我的最愛(ài)  
本文給大家分享的小技巧是如何利用Python腳本獲取Windows和Linux的系統(tǒng)版本信息,非常的簡(jiǎn)單實(shí)用,有需要的小伙伴可以查看下

查看系統(tǒng)版本信息是一件家常便飯的事情,有時(shí)候需要將版本信息錄入到資產(chǎn)管理系統(tǒng)中,如果每次手動(dòng)的去查詢這些信息再錄入系統(tǒng)那么是一件令人呢頭疼的事情,如果采用腳本去完成這件事情,那么情況就有所不同了。

在Python的世界里,獲取Windows版本信息和Linux的版本信息都可以采用platform模塊,但platform模塊也不是萬(wàn)能的,有些特殊的信息(比如Windows的內(nèi)部版本號(hào))這個(gè)模塊拿不到,那么只能另辟蹊徑了。

在Linux系統(tǒng)中,可以簡(jiǎn)單的認(rèn)為一切都是文件,那么就算沒(méi)有現(xiàn)成的命令可用時(shí),可以用open()文件的方法通過(guò)對(duì)文件的讀寫(xiě)控制它。而在Windows的大部分信息在注冊(cè)表中都能查到,因此可以從注冊(cè)表上下手。Windows注冊(cè)表是一個(gè)好東西,拿數(shù)據(jù)就像在Linux下一切都是文件一樣方便,如果想用Python訪問(wèn)注冊(cè)表,除了權(quán)限外就是需要模塊了,在Python中_winreg是一個(gè)內(nèi)置模塊,通過(guò)這一模塊可以對(duì)注冊(cè)表進(jìn)行讀寫(xiě)。

本腳本收集了一些獲取版本信息的常見(jiàn)方法,除了platform模塊外,還有其他的模塊可供使用,因?yàn)閜latform模塊不是內(nèi)置模塊,因此需要額外安裝。Windows下運(yùn)行腳本需要考慮權(quán)限問(wèn)題和中文字符的問(wèn)題,解決Python打印中文字符的問(wèn)題是通過(guò)腳本中的get_system_encoding()函數(shù)實(shí)現(xiàn)的,這個(gè)函數(shù)取自Django,經(jīng)過(guò)測(cè)試這個(gè)函數(shù)還是非常好用的。

注:在PyCharm中,經(jīng)常遇到Run窗口打印出的中文顯示亂碼,代碼中沒(méi)有經(jīng)過(guò)正確轉(zhuǎn)碼是一方面,而IDE的編碼設(shè)置也是一方面。如果是在Windows下開(kāi)發(fā),那么建議代碼用UTF-8編寫(xiě),IDE的編碼則設(shè)置為“GBK”,設(shè)置方法“File”-->"Settings"-->"Editor"-->"File Encoding"-->"IDE Encoding"選擇“<System Default (now GBK)>”, "Project Encoding"選擇UTF-8保證代碼的編碼一致性。

腳本如下:

#!/usr/bin/python
# encoding: utf-8
# -*- coding: utf8 -*-
"""
Created by PyCharm.
File:        LinuxBashShellScriptForOps:getSystemVersion.py
User:        Guodong
Create Date:    2016/12/16
Create Time:    14:51
 """
import sys
import os
import platform
import subprocess
import codecs
import locale


def get_system_encoding():
  """
  The encoding of the default system locale but falls back to the given
  fallback encoding if the encoding is unsupported by python or could
  not be determined. See tickets #10335 and #5846
  """
  try:
    encoding = locale.getdefaultlocale()[1] or 'ascii'
    codecs.lookup(encoding)
  except Exception:
    encoding = 'ascii'
  return encoding


DEFAULT_LOCALE_ENCODING = get_system_encoding()

mswindows = (sys.platform == "win32") # learning from 'subprocess' module
linux = (sys.platform == "linux2")

hidden_hostname = True

if mswindows:
  uname = list(platform.uname())
  if hidden_hostname:
    uname[1] = "hidden_hostname"
  print uname

  import _winreg

  try:
    reg_key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion")
    if reg_key:
      ProductName = _winreg.QueryValueEx(reg_key, "ProductName")[0] or None
      EditionId = _winreg.QueryValueEx(reg_key, "EditionId")[0] or None
      ReleaseId = _winreg.QueryValueEx(reg_key, "ReleaseId")[0] or None
      CurrentBuild = _winreg.QueryValueEx(reg_key, "CurrentBuild")[0] or None
      BuildLabEx = _winreg.QueryValueEx(reg_key, "BuildLabEx")[0][:9] or None
      print (ProductName, EditionId, ReleaseId, CurrentBuild, BuildLabEx)
  except Exception as e:
    print e.message.decode(DEFAULT_LOCALE_ENCODING)

if linux:
  uname = list(platform.uname())
  if hidden_hostname:
    uname[1] = "hidden_hostname"
  print uname

  proc_obj = subprocess.Popen(r'uname -a', shell=True, stdout=subprocess.PIPE,
                stderr=subprocess.STDOUT)
  result = proc_obj.stdout.read().strip().decode(DEFAULT_LOCALE_ENCODING)
  if result:
    print result

  if os.path.isfile("/proc/version"):
    with open("/proc/version", 'r') as f:
      content = f.read().strip()
    if content != "":
      print content

  if os.path.isfile("/etc/issue"):
    with open("/etc/issue", 'r') as f:
      content = f.read().strip()
    if content != "":
      print content

截圖如下:

(1)注冊(cè)表信息獲取位置:

(2)Windows環(huán)境下的輸出:

(3)Linux環(huán)境下的輸出:

相關(guān)文章

最新評(píng)論

通州区| 石阡县| 武夷山市| 文水县| 江山市| 涟水县| 高青县| 梁山县| 昭苏县| 静宁县| 张北县| 盐池县| 尼勒克县| 敦化市| 五常市| 积石山| 麻栗坡县| 龙州县| 宝清县| 永登县| 济源市| 古交市| 类乌齐县| 维西| 西贡区| 阳江市| 盐池县| 大同县| 沈丘县| 乐至县| 闻喜县| 贺州市| 香港| 上虞市| 阿坝县| 英吉沙县| 乐安县| 永靖县| 黎城县| 邳州市| 铜梁县|