天天看點

Python實作系統時間自動校正

最近由于桌上型電腦CMOS電池沒電了,每次開機後系統時間都會被初始化。出于每次都要重新設定系統時間太麻煩的考慮,今天用Python實作了系統開機自動校正時間的程式。大緻的思路是:首先産生一個windows service,該service在被啟動之後通過urllib獲得标準中原標準時間,并根據标準時間校對系統時間。具體代碼如下:

#SyncLocaltime.py
#-*-coding=gbk-*-
import urllib
import re
import os 
import time
import win32serviceutil
import win32service
import win32event
import win32evtlogutil

url = "http://www.beijing-time.org/time.asp"

def get_information(url):
    try:
        wp = urllib.urlopen( url ) 
    except :
        return [] 
    ch = wp.read()
    ch1 = re.sub( r'\s' , "" , ch )
    ch2 = ch1.split(";") 
    return ch2

def get_time():
    ans = [] 
    while True :
        ch = get_information( url );
        if len(ch) == 0 :
            print "30秒後将重新連接配接"
            time.sleep( 30 ) 
        else :
            break; 
    for i in range(1 , len( ch )-1 ):
        ss = re.search( r'\d' , ch[i] ).start()
        needed = ch[i][ss : len( ch[i] ) ]
        ans.append( int(needed) )
    os.system("date %d-%d-%d" % ( ans[0] , ans[1] , ans[2]) )
    os.system("time %d:%d:%d.0" % ( ans[4] , ans[5] ,ans[6]) )

class SyncLocaltime(win32serviceutil.ServiceFramework): #類名要與檔案名一緻
    _svc_name_ = "SyncLocaltime"    #服務名稱
    _svc_display_name_ = "SyncLocaltime"
    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
        print '服務開始'
    
    def SvcDoRun(self):
        import servicemanager
        #------------------------------------------------------
        # Make entry in the event log that this service started
        #------------------------------------------------------
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,servicemanager.PYS_SERVICE_STARTED,(self._svc_name_, ''))
        #-------------------------------------------------------------
        # Set an amount of time to wait (in milliseconds) between runs
        #-------------------------------------------------------------
        self.timeout=100
        while 1:
            #-------------------------------------------------------
            # Wait for service stop signal, if timeout, loop again
            #-------------------------------------------------------
            rc=win32event.WaitForSingleObject(self.hWaitStop,self.timeout)
            #
            # Check to see if self.hWaitStop happened
            #
            if rc == win32event.WAIT_OBJECT_0:
                #
                # Stop signal encountered
                #
                break
            else:
                #
                # Put your code here
                #
                get_time() 
                break 
                print '服務運作中'
                time.sleep(1)
        #
        # Only return from SvcDoRun when you wish to stop
        #
        return
        
    def SvcStop(self):
#---------------------------------------------------------------------
        # Before we do anything, tell SCM we are starting the stop process.
#---------------------------------------------------------------------
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
#---------------------------------------------------------------------
        # And set my event
#---------------------------------------------------------------------
        win32event.SetEvent(self.hWaitStop)
        print '服務結束'
        return
    
if __name__=='__main__':
        win32serviceutil.HandleCommandLine( SyncLocaltime )
           

為了讓使用者在沒有python的環境下也可以運作該程式,這裡用py2exe子產品将py程式轉換為exe檔案。轉換過程隻需要在SyncLocaltime.py檔案的目錄下建立一個setup.py腳本,代碼為:

# setup.py
from distutils.core import setup
import py2exe

setup(
	# The first three parameters are not required, if at least a
	# 'version' is given, then a versioninfo resource is built from
	# them and added to the executables.
	version = "0.0.1",
	description = "Synchroniz local system time with beijin time",
	name = "SyncLocaltime",

	# targets to build
	service=["SyncLocaltime"]
)
           

這兩個檔案都建立好之後,就可以生成exe檔案,并将服務添加都windows服務項中并啟動服務了。具體批處理檔案為:

python setup.py py2exe       //将SyncLocaltime.py轉換為exe檔案,生成build和dist檔案夾,SyncLocaltime.exe檔案位于dist中
pause
cd dist				
SyncLocaltime.exe -install	//安裝服務,将服務添加到windows服務項中
pause
           

執行完上述批處理檔案之後,我們就可以在運作->services.msc中打開系統服務,在其中查找一個叫做“SyncLocaltime”的服務,該服務現在預設是手動,并且未啟動的,可以通過右鍵->啟動,來啟動該服務,服務啟動之後就會将系統時間設定為北京标準時間。

這樣在服務安裝好之後,我們還需要在服務中手動啟動服務,顯然還不夠便捷。這裡我們可以在dist添加兩個批處理檔案startService.bat和stopService.bat來達到上述的效果。具體代碼為:

#startService.bat
@echo off 
echo 正在安裝服務,請稍候...
SyncLocaltime.exe -install

echo 正在啟動服務...
sc config SyncLocaltime start= auto
sc start SyncLocaltime

echo 服務啟動成功
pause

#stopService.bat
@echo off
echo 正在停止服務,請稍候...
sc stop SyncLocaltime

echo 正在解除安裝服務...
SyncLocaltime.exe -remove

echo 服務解除安裝成功
pause
           

到這裡我們就可以将dist檔案打包使用了,在使用的時候startService.bat用于啟動服務,stopService.bat用于關閉服務。

但是作為一個軟體,這樣會顯得比較寒顫,我們最好能将dist檔案打包成一個setup.exe。使用者隻需要下載下傳setup.exe安裝包,然後安裝成功就可以實作系統時間自動校正的功能。這裡我使用NINS工具對dist進行打包。

首先需要下載下傳:NINS 和 NIS Edit這兩個軟體,然後對軟體進行安裝。具體的過程可以參考:NINS打包圖文教程。最後生成一個setup.exe就是使用者要下載下傳的安裝包。

至此,windows系統時間的自動校正已圓滿解決。