Python gets the standard Beijing time method

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.