This example describes Python's approach to obtaining standard Beijing time. Share to everyone for your reference. The specific analysis is as follows:
This Python code is mainly through Www.beijing-time.org's official website to get the standard Beijing time, if your server hangs on the net, you can use this code to regularly get Beijing time, and then update the standard time of your system.
Import Time,httplibdef 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 = =: 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]< c12/>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 noneif __name__== ' __main__ ': print ( Getbeijintime ())
Hopefully this article will help you with Python programming.