• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            清風(fēng)竹林

            ぷ雪飄絳梅映殘紅
               ぷ花舞霜飛映蒼松
                 ----- Do more,suffer less

            網(wǎng)絡(luò)校時(shí) python

               由于手動(dòng)調(diào)整和計(jì)算機(jī)時(shí)鐘精度的問題,我的windows時(shí)間經(jīng)常需要校正。以前用c#寫過一個(gè)ntp的時(shí)間校正工具,為此從網(wǎng)上找了好多ntp server的ip地址,但最終發(fā)現(xiàn)漸漸的它們都不能用了。后來慢慢地感覺到自己對(duì)系統(tǒng)時(shí)鐘的要求也沒那么精確,于是開始打web server的主意:通常web server,特別是一些比較著名的網(wǎng)站的server總有一些是在線的。
              代碼似乎也不是很復(fù)雜,基于python 3.0---
            import time
            import urllib.request
            import urllib.parse
            import ctypes


            SetSystemTime 
            = ctypes.windll.kernel32.SetSystemTime
            GetSystemTime 
            = ctypes.windll.kernel32.GetSystemTime

            class SYSTEMTIME(ctypes.Structure):
                c_ushort
            = ctypes.c_ushort
                _fields_ 
            =  (
                            (
            'wYear', c_ushort), 
                            (
            'wMonth', c_ushort), 
                            (
            'wDayOfWeek', c_ushort), 
                            (
            'wDay', c_ushort), 
                            (
            'wHour', c_ushort), 
                            (
            'wMinute', c_ushort), 
                            (
            'wSecond', c_ushort), 
                            (
            'wMilliseconds', c_ushort), 
                            )
                
            def __str__(self):
                    
            return '%4d%02d%02d%02d%02d%02d.%03d' % (self.wYear,self.wMonth,self.wDay,self.wHour,self.wMinute,self.wSecond,self.wMilliseconds)

            def updateSystemTime():
                url
            = 'http://www.baidu.com'
                
            try:
                    response
            = urllib.request.urlopen(url)
                    header
            = response.info()
                    date
            =header['Date']
                    gmt
            =time.strptime(date[5:25], "%d %b %Y %H:%M:%S")
                    st
            =SYSTEMTIME(gmt.tm_year,gmt.tm_mon,gmt.tm_wday,gmt.tm_mday,gmt.tm_hour,gmt.tm_min,gmt.tm_sec,0)
                    SetSystemTime(ctypes.byref(st))
                    
            print('網(wǎng)絡(luò)校時(shí)成功!')
                
            except Exception as ex:
                    
            print(ex)
                    
            print('網(wǎng)絡(luò)校時(shí)失敗')
                    
            return False
                
            return True
            def printCurTime():
                now
            = time.localtime(time.time())
                
            print('當(dāng)前系統(tǒng)時(shí)間:', time.strftime("%Y-%m-%d %H:%M:%S", now))
                
            if __name__=='__main__':
                printCurTime()
                
            if updateSystemTime():
                    printCurTime()
              保存文件名為updateTime.py,放入某個(gè)系統(tǒng)目錄,在命令行中測(cè)試如下:

            C:\>updatetime
            當(dāng)前系統(tǒng)時(shí)間: 2009-12-16 16:51:11
            網(wǎng)絡(luò)校時(shí)成功!
            當(dāng)前系統(tǒng)時(shí)間: 2008-12-16 16:51:12

            C:\>

             之前,我故意把時(shí)間調(diào)整到了2009年。

              

            posted on 2008-12-16 16:53 李現(xiàn)民 閱讀(874) 評(píng)論(0)  編輯 收藏 引用 所屬分類: minitools

            色综合久久88色综合天天 | 日日狠狠久久偷偷色综合0| 久久综合亚洲欧美成人| 99久久精品日本一区二区免费| 久久A级毛片免费观看| 一级做a爰片久久毛片人呢| 伊人久久大香线蕉AV一区二区| 亚洲狠狠婷婷综合久久久久| jizzjizz国产精品久久| 人妻无码精品久久亚瑟影视| 久久久久99精品成人片试看 | 久久久国产精华液| 无码AV中文字幕久久专区| 99热精品久久只有精品| 无码AV中文字幕久久专区| 色8激情欧美成人久久综合电| 99久久国产综合精品麻豆| 久久人人爽人人人人爽AV| 国产精品99久久久久久宅男| 狠狠88综合久久久久综合网 | 精品综合久久久久久97超人| 久久精品成人欧美大片| 国产日产久久高清欧美一区| 婷婷久久久亚洲欧洲日产国码AV | 久久久久99这里有精品10| 国产精品视频久久| 久久久久亚洲AV无码永不| 久久频这里精品99香蕉久| 人妻丰满?V无码久久不卡| 99久久免费只有精品国产| 久久99精品国产99久久6男男| 精品国产乱码久久久久久1区2区 | 伊人情人综合成人久久网小说 | 亚洲AV乱码久久精品蜜桃| 久久亚洲AV无码西西人体| 国产精品无码久久四虎| 久久综合中文字幕| 大美女久久久久久j久久| 国内精品久久久久久中文字幕| 久久九九亚洲精品| 久久国产精品二国产精品|