python获取网络时间和本地时间

今天我们来看一下如何用python获取网络时间和本地时间,直接上代码吧,代码中都有注释。

python获取网络时间

获取网络时间 def 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()             logging.debug(result)             data = result.split("")             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:         logging.exception("getBeijinTime except")         return None

登录后复制

python获取本地时间

同步本地系统时间 def syncLocalTime():     """     同步本地时间     """     logging.info("current local time is: %d-%d-%d %d:%d:%d" % time.localtime()[:6])            beijinTime = getBeijinTime()     if beijinTime is None:         logging.info("get beijinTime is None, will try again in 30 seconds...")         timer = threading.Timer(30.0, syncLocalTime)         timer.start();     else:         logging.info("get beijinTime is: %d-%d-%d %d:%d:%d" % beijinTime[:6])                        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))    #设置时间         logging.info("syncLocalTime complete, current local time: %d-%d-%d %d:%d:%d " %time.localtime()[:6])

登录后复制

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2283465.html

(0)
上一篇 2025年2月27日 20:09:44
下一篇 2025年2月17日 23:42:45

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • Python集合(set)类型的操作总结

    python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和sysmmetric differenc…

    编程技术 2025年2月27日
    100
  • python网络编程中常用到的函数​总结

    总结一下python网络编程中常用到的函数 socket.getservbyname(servicename[, protocolname]) –> integer   查询某个协议对应的端口号,需要使用两个参数,servicenam…

    编程技术 2025年2月27日
    200
  • python 序列化之JSON和pickle详解

    JSON模块 json(javascript object notation) 是一种轻量级的数据交换格式。它基于ecmascript的一个子集。 json采用完全独立于语言的文本格式,但是也使用了类似于c语言家族的习惯(包括c、c++、j…

    2025年2月27日
    200
  • Python3获取大量电影信息

    实验室这段时间要采集电影的信息,给出了一个很大的数据集,数据集包含了4000多个电影名,需要我写一个爬虫来爬取电影名对应的电影信息。  其实在实际运作中,根本就不需要爬虫,只需要一点简单的Python基础就可以了。  前置需求: Pytho…

    2025年2月27日 编程技术
    200
  • Python字符串

    如下学习python的字符串用法。 print(dir(str)) [‘__add__’, ‘__class__’, ‘__contains__’, ‘__…

    编程技术 2025年2月27日
    200
  • python三级菜单

    menu = {    ‘北京’:{        ‘海淀’:{            ‘五道口’:{                ‘soho’:{},                ‘网易’:{},                ‘go…

    编程技术 2025年2月27日
    200
  • python杂记

    os模块说明:python os模块包含普遍的操作系统功能 os.access(path, mode) # 检验权限模式 os.chdir(path) # 改变当前工作目录os.chflags(path, flags) # 设置路径的标记为…

    编程技术 2025年2月27日
    200
  • 详解Python的装饰器

    python中的装饰器是你进入python大门的一道坎,不管你跨不跨过去它都在那里。 为什么需要装饰器 我们假设你的程序实现了say_hello()和say_goodbye()两个函数。 def say_hello():     print…

    编程技术 2025年2月27日
    200
  • Python模块:logging

    很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误、警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,logging的日志可以分为 debug、info、…

    2025年2月27日
    100
  • Python网络编程

    认识Socket socket通常也称作”套接字”,用于描述ip地址和端口,是一个通信链的句柄,应用程序通常通过”套接字”向网络发出请求或者应答网络请求。 socket起源于Unix,而Uni…

    2025年2月27日
    200

发表回复

登录后才能评论