• <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

            亚洲女久久久噜噜噜熟女| 久久久久国产精品麻豆AR影院| 亚洲综合精品香蕉久久网| 精品久久8x国产免费观看| 久久99精品久久久久久齐齐| 午夜精品久久久久| 久久久久四虎国产精品| 天天综合久久一二三区| 欧美亚洲国产精品久久蜜芽| 久久久久久久精品成人热色戒 | 久久超乳爆乳中文字幕| 99久久精品无码一区二区毛片| 久久国产精品无| 国产成人久久久精品二区三区| 香蕉久久夜色精品升级完成| 久久久久无码专区亚洲av| 99久久精品午夜一区二区 | jizzjizz国产精品久久| 亚洲国产成人精品久久久国产成人一区二区三区综 | 久久综合给合综合久久| 少妇无套内谢久久久久| 99久久精品国产综合一区| 看久久久久久a级毛片| 久久久久国产| 久久影视综合亚洲| 99热都是精品久久久久久| 久久久久久久人妻无码中文字幕爆 | 久久无码人妻一区二区三区| 日韩人妻无码一区二区三区久久99| 久久精品www| 91视频国产91久久久| 狠狠色丁香婷婷久久综合五月| 国产精品美女久久久免费| 999久久久免费国产精品播放| 99久久中文字幕| 国内精品久久久久久99| 久久99国内精品自在现线| 国内精品久久久久久99| 97精品国产97久久久久久免费| 久久久精品免费国产四虎| 94久久国产乱子伦精品免费|