Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use http time synchronization to set system time source code by Python

Shulou Source: shulou.com Published: 2022-06-03 00:08:28 09月15日 Update

This article introduces Python how to use http time synchronization to set up the system time source code, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Python mode uses http time synchronization to set the system time source code, the system environment is ubuntu 12.04, Python2.7 version. The time, os, and httplib methods are required.

Python uses http time synchronization to set the system time. The source codes are as follows:

`#-*-coding:utf8-*-``import``time`import``os`def``get_webservertime (host): ``conn`` = ``client.HTTPConnection (host) ``conn.request (```client.HTTPConnection (host) ``r`` = `conn.getresponse () ``ts`` = ```getresponse () ``ts`` = ```date' ``) `# get the http header date ````# convert GMT time to Beijing time ``time`` = ``time.mktime (time.strptime (ts``5``) ``"d% b% Y% H:%M:%S GMT"``) ``+`` (``8`` * ``60`` * `60``) ``ltime`` = ``time.gmtime (local_time) ``# use date to set the time ``dat`` = ```date-u-s "% d-%d-%d% d:%d:%d" ``%`` (ltime.tm_year,ltime.tm_mon,ltime.tm_mday,ltime.tm_hour,ltime.tm_min) Ltime.tm_sec) ``os.system (dat) ``get_webservertime (``www.baidu.com' ``) `about how Python uses http time synchronization to set up the system time source code here. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Time system source code synchronization content more help good interest guys buddies articles methods methods versions environment knowledge articles parts Beijing Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information vpn macOS Redmi Apple