Python obtains the standard Beijing Time, And python obtains the standard Beijing time.

Source: Internet
Author: User

Python obtains the standard Beijing Time, And python obtains the standard Beijing time.

This article describes how to obtain the standard Beijing Time Using python. Share it with you for your reference. The specific analysis is as follows:

This section of python code mainly through the www.beijing-time.org official website to get the standard Beijing time, if your server is hanging online, you can through this code regularly get Beijing time, and then update your system's Standard Time.

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 == 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 Noneif __name__=='__main__':  print(getBeijinTime())

I hope 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.