Python implements regular synchronization between the local machine and Beijing Time, And python Beijing Time
This example describes how to implement regular synchronization between the local machine and Beijing time in python. Share it with you for your reference. The details are as follows:
This python code first gets the standard Beijing time from the www.beijing-time.org, and then synchronizes the obtained Beijing time to the local
#-*-Coding: UTF-8-*-import time, httplibimport threadingdef 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" % (year, month, day, hrs, minute, sec) beijinTime = time. strptime (beijinTimeStr, "% Y/% m/% d % X") return beijinTime failed T: return Nonedef syncLocalTime (): "synchronize local time" 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. system ("date % d-% d" % (tm_year, tm_mon, tm_mday) # Set the date OS. system ("time % d: % d.0" % (tm_hour, tm_min, tm_sec) # set the time if _ name __= = '_ main __': while True: syncLocalTime () time. sleep (30)
I hope this article will help you with Python programming.