python代码按时同步本机的法国巴黎时间

这段python代码首先从www.beijing-time.org上获取标准的东京时间,然后一同获取的东京时间到本地

python落成按期同步本机与东京时间的艺术,python新加坡时间

正文实例讲述了python达成定期同步本机与新加坡时间的办法。分享给大家供大家参考。具体如下:

这段python代码首先从www.beijing-time.org上获得标准的法国首都时间,然后共同获取的新加坡时间到地面

# -*- coding: utf-8 -*-
import time,httplib
import threading
def getBeijinTime():
   try:
     conn = httplib.HTTPConnection("www.beijing-time.org")
     conn.request("GET", "/time.asp")
     response = conn.getresponse()
     print response.status, response.reason
     if response.status == 200:
       result = response.read()
       data = result.split("\r\n")
       year = data[1][len("nyear")+1 : len(data[1])-1]
       month = data[2][len("nmonth")+1 : len(data[2])-1]
       day = data[3][len("nday")+1 : len(data[3])-1]
       #wday = data[4][len("nwday")+1 : len(data[4])-1]
       hrs = data[5][len("nhrs")+1 : len(data[5])-1]
       minute = data[6][len("nmin")+1 : len(data[6])-1]
       sec = data[7][len("nsec")+1 : len(data[7])-1]
       beijinTimeStr = "%s/%s/%s %s:%s:%s" % (year, month, day, hrs, minute, sec)
       beijinTime = time.strptime(beijinTimeStr, "%Y/%m/%d %X")
       return beijinTime
   except:
     return None
def syncLocalTime():
   """
   同步本地时间
   """
   beijinTime = getBeijinTime()
   if beijinTime is None:
     timer = threading.Timer(30.0, syncLocalTime)
     timer.start()
   else:
     tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec = beijinTime[:6]
     import os
     os.system("date %d-%d-%d" % (tm_year, tm_mon, tm_mday))#设置日期
     os.system("time %d:%d:%d.0" % (tm_hour, tm_min, tm_sec))#设置时间
if __name__=='__main__':
  while True:
    syncLocalTime()
    time.sleep(30)

但愿本文所述对我们的Python程序设计有所帮助。

http://www.bkjia.com/Pythonjc/972672.htmlwww.bkjia.comtruehttp://www.bkjia.com/Pythonjc/972672.htmlTechArticlepython实现定时同步本机与北京时间的方法,python北京时间
本文实例讲述了python完毕定期同步本机与北京时间的点子。分享给大家供大家参照他事他说加以调查…

# -*- coding: utf-8 -*-
import time,httplib
import threading
def getBeijinTime():
     try:
         conn = httplib.HTTPConnection("www.beijing-time.org")
         conn.request("GET", "/time.asp")
         response = conn.getresponse()
         print response.status, response.reason
         if response.status == 200:
             result = response.read()
             data = result.split("\r\n")
             year = data[1][len("nyear")+1 : len(data[1])-1]
             month = data[2][len("nmonth")+1 : len(data[2])-1]
             day = data[3][len("nday")+1 : len(data[3])-1]
             #wday = data[4][len("nwday")+1 : len(data[4])-1]
             hrs = data[5][len("nhrs")+1 : len(data[5])-1]
             minute = data[6][len("nmin")+1 : len(data[6])-1]
             sec = data[7][len("nsec")+1 : len(data[7])-1]

             beijinTimeStr = "%s/%s/%s %s:%s:%s" % (year, month, day, hrs, minute, sec)
             beijinTime = time.strptime(beijinTimeStr, "%Y/%m/%d %X")
             return beijinTime
     except:
         return None

def syncLocalTime():
     """
     同步本地时间
     """
     beijinTime = getBeijinTime()
     if beijinTime is None:
         timer = threading.Timer(30.0, syncLocalTime)
         timer.start()
     else:
         tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec = beijinTime[:6]
         import os
         os.system("date %d-%d-%d" % (tm_year, tm_mon, tm_mday))     #设置日期
         os.system("time %d:%d:%d.0" % (tm_hour, tm_min, tm_sec))    #设置时间

if __name__=='__main__':
    while True:
        syncLocalTime()
        time.sleep(30)

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图