python代码定时同步本机的北京时间,python北京时间,这段python代码首先
python代码定时同步本机的北京时间,python北京时间,这段python代码首先
这段python代码首先从www.beijing-time.org上获取标准的北京时间,然后同步获取的北京时间到本地
# -*- 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 %s:%s:%s" % (year, month, day, hrs, minute, sec) beijinTime = time.strptime(beijinTimeStr, "%Y/%m/%d %X") return beijinTime except: return Nonedef syncLocalTime(): """ 同步本地时间 """ 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 os.system("date %d-%d-%d" % (tm_year, tm_mon, tm_mday)) #设置日期 os.system("time %d:%d:%d.0" % (tm_hour, tm_min, tm_sec)) #设置时间if __name__=='__main__': while True: syncLocalTime() time.sleep(30)
相关内容
- bottle---Python的轻量级http server,,相比于Django而言,
- 一个简单python ftp客户端代码,pythonftp客户端,#!/usr/bin
- Python3.4 邮件发送(含带中文附件),python3.4中文,None
- python通过ftp上传文件,,引入ftplib模块通过
- 帮同事写的一个切换数据库启动的小工具,同事切换数
- python检测远程udp端口是否打开,python检测udp端口,pytho
- 分析并输出Python代码依赖的库,输出python代码依赖,#!
- 文件按照日期命名,文件日期命名,# -*- coding
- python判断远程端口是否打开,python判断端口,python判断远
- python helloworld,helloworld,#!/usr/bin/e
评论关闭