码迷,mamicode.com
首页 > 编程语言 > 详细

python获取标准的北京时间的源码

时间:2020-07-15 13:09:42      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:color   request   http   ref   nload   down   pypi   ptime   www   

--

安装方法1

下载httplib库:https://pypi.org/project/httplib2/#downloads

httplib2-0.18.1-py3-none-any.whl 

安装:pip install xx.whl

安装方法2

下载httplib库:https://pypi.org/project/httplib2/#downloads

httplib2-0.18.1.tar

解压,进入该目录

>python setup.py install

 

代码:

import time,httplib
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()
             data = result.split("rn")
             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 None


if __name__==__main__:
    print(getBeijinTime())

 

python获取标准的北京时间的源码

标签:color   request   http   ref   nload   down   pypi   ptime   www   

原文地址:https://www.cnblogs.com/Ph-one/p/13304363.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!