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

            清風竹林

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

            網絡校時 python

               由于手動調整和計算機時鐘精度的問題,我的windows時間經常需要校正。以前用c#寫過一個ntp的時間校正工具,為此從網上找了好多ntp server的ip地址,但最終發現漸漸的它們都不能用了。后來慢慢地感覺到自己對系統時鐘的要求也沒那么精確,于是開始打web server的主意:通常web server,特別是一些比較著名的網站的server總有一些是在線的。
              代碼似乎也不是很復雜,基于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('網絡校時成功!')
                
            except Exception as ex:
                    
            print(ex)
                    
            print('網絡校時失敗')
                    
            return False
                
            return True
            def printCurTime():
                now
            = time.localtime(time.time())
                
            print('當前系統時間:', time.strftime("%Y-%m-%d %H:%M:%S", now))
                
            if __name__=='__main__':
                printCurTime()
                
            if updateSystemTime():
                    printCurTime()
              保存文件名為updateTime.py,放入某個系統目錄,在命令行中測試如下:

            C:\>updatetime
            當前系統時間: 2009-12-16 16:51:11
            網絡校時成功!
            當前系統時間: 2008-12-16 16:51:12

            C:\>

             之前,我故意把時間調整到了2009年。

              

            posted on 2008-12-16 16:53 李現民 閱讀(873) 評論(0)  編輯 收藏 引用 所屬分類: minitools

            精品国产乱码久久久久久人妻| 久久精品人成免费| 精品久久久久久国产| 2021少妇久久久久久久久久| 一本久久a久久精品综合夜夜| 精品多毛少妇人妻AV免费久久| 色欲综合久久躁天天躁| 伊人久久综合精品无码AV专区| 日韩精品久久久久久免费| 久久精品人人做人人爽电影| 久久国产精品波多野结衣AV| 久久久久久精品久久久久| 国产精品岛国久久久久| 久久亚洲精品无码观看不卡| 久久亚洲中文字幕精品有坂深雪| 精品国产青草久久久久福利| 亚洲AV日韩精品久久久久久| 麻豆久久| 久久久久国产精品| 99久久精品免费看国产一区二区三区| 99久久免费国产精品| 色偷偷偷久久伊人大杳蕉| 伊人热热久久原色播放www| www.久久热| 人妻精品久久无码区| 亚洲国产成人精品女人久久久 | 国产精品岛国久久久久| 久久只有这里有精品4| 国产亚洲婷婷香蕉久久精品| 久久亚洲精品国产亚洲老地址| 亚洲国产精品婷婷久久| 无码久久精品国产亚洲Av影片 | 99久久精品九九亚洲精品| 偷偷做久久久久网站| 综合久久给合久久狠狠狠97色| 久久成人精品| 久久久久噜噜噜亚洲熟女综合| 国产精品热久久无码av| 久久综合九色综合久99| 99999久久久久久亚洲| 久久精品国产亚洲AV高清热 |