python如何获取淘宝服务器时间(附详细代码)

python获取淘宝服务器时间的代码示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧。

直接上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import requests
import time
while True:
    class timeTaobao(object):
        r1 = requests.get(url='http://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp',
                      headers={'User-Agent': 'Mozilla/5.0
(Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/55.0.2883.87 UBrowser/6.2.4098.3 Safari/537.36'
})
        x = eval(r1.text)
        timeNum = int(x['data']['t'])
        def funcname():
            timeStamp = float(timeTaobao.timeNum/1000)
            timeArray = time.localtime(timeStamp)
            otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
            return otherStyleTime
    t = timeTaobao.funcname()
    print(t)

结果

2021-04-20 15:30:04
2021-04-20 15:30:04
2021-04-20 15:30:04
2021-04-20 15:30:04
2021-04-20 15:30:04
2021-04-20 15:30:05
2021-04-20 15:30:05
2021-04-20 15:30:05
2021-04-20 15:30:05
2021-04-20 15:30:05
2021-04-20 15:30:05

补充:【Python】获取服务器时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import http.client
import time
import os
def get_webservertime(host):
    conn=http.client.HTTPConnection(host)
    conn.request("GET", "/")
    r=conn.getresponse()
    #r.getheaders() #获取所有的http头
    ts=  r.getheader('date') #获取http头date部分
    print(ts)
     
    #将GMT时间转换成北京时间
    ltime= time.strptime(ts[5:25], "%d %b %Y %H:%M:%S")
    print(ltime)
    ttime=time.localtime(time.mktime(ltime)+8*60*60)
    print(ttime)
    dat="%u-%02u-%02u"%(ttime.tm_year,ttime.tm_mon,ttime.tm_mday)
    tm="%02u:%02u:%02u"%(ttime.tm_hour,ttime.tm_min,ttime.tm_sec)
    print (dat,tm)
    os.system(dat)
    os.system(tm)
     
get_webservertime('www.jd.com')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import urllib.request
import time
def get_webservertime(url):
   #返回一个对象
    response=urllib.request.urlopen(url)
    #打印出远程服务器返回的header信息
    #print (response.info())
    header=response.info()
  
    ts=header._headers[1][1]
     
    #将GMT时间转换成北京时间
    ltime= time.strptime(ts[5:25], "%d %b %Y %H:%M:%S")
    ttime=time.localtime(time.mktime(ltime)+8*60*60)
    dat="%u-%02u-%02u"%(ttime.tm_year,ttime.tm_mon,ttime.tm_mday)
    tm="%02u:%02u:%02u"%(ttime.tm_hour,ttime.tm_min,ttime.tm_sec)
    print (dat,tm)
 
get_webservertime('https://www.jd.com/')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import http.client
import time
def get_webservertime(host):
    while True:
        try:
            conn=http.client.HTTPConnection(host)
            conn.request("GET", "/")
            r=conn.getresponse()
            ts=  r.getheader('date') #获取http头date部分
            break
        except Exception as e:
            print(e)
            continue
    #将GMT时间转换成北京时间
    ltime= time.strptime(ts[5:25], "%d %b %Y %H:%M:%S")
    ttime=time.localtime(time.mktime(ltime)+8*60*60)
    dat="%u-%02u-%02u"%(ttime.tm_year,ttime.tm_mon,ttime.tm_mday)
    tm="%02u:%02u:%02u"%(ttime.tm_hour,ttime.tm_min,ttime.tm_sec)
    timeStr=dat+' '+tm
    return timeStr
    
      
url='www.jd.com'
while True:
    print(get_webservertime(url))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
def get_webservertime():
    url='https://ai.jd.com/jdip/useripinfo.php?callback=jsonpCallbackUserIpInfo'
    while True:
        try:
            response=urllib.request.urlopen(url)
            header=response.info()
            break
        except Exception as e:
            print(e)
            time.sleep(1)
            continue
          
    #打印出远程服务器返回的header信息
    ts=header._headers[1][1]
    #将GMT时间转换成北京时间
    ltime= time.strptime(ts[5:25], "%d %b %Y %H:%M:%S")
    ttime=time.localtime(time.mktime(ltime)+8*60*60)
    dat="%u-%02u-%02u"%(ttime.tm_year,ttime.tm_mon,ttime.tm_mday)
    tm="%02u:%02u:%02u"%(ttime.tm_hour,ttime.tm_min,ttime.tm_sec)
    timeStr=dat+' '+tm
    return timeStr

以上就是关于python获取淘宝服务器时间的代码示例了,有需要的朋友可以试试。

未经允许不得转载:搬瓦工评测网 » python如何获取淘宝服务器时间(附详细代码)