欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python獲取網(wǎng)絡(luò)時(shí)間戳的兩種方法詳解

 更新時(shí)間:2022年01月13日 09:43:41   作者:幸福的達(dá)哥  
在我們進(jìn)行注冊碼的有效期驗(yàn)證時(shí),通常使用獲取網(wǎng)絡(luò)時(shí)間的方式來進(jìn)行比對。本文將介紹兩種利用Python獲取網(wǎng)絡(luò)時(shí)間戳的方法,感興趣的可以了解一下

在我們進(jìn)行注冊碼的有效期驗(yàn)證時(shí),通常使用獲取網(wǎng)絡(luò)時(shí)間的方式來進(jìn)行比對。

以下為獲取網(wǎng)絡(luò)時(shí)間的幾種方式。

方法一

需要的時(shí)間會比較長,個(gè)別電腦上可能會出現(xiàn)不兼容現(xiàn)象

代碼實(shí)現(xiàn)

 def get_web_server_time(self, host_URL, year_str='-', time_str=':'):
        '''
        獲取網(wǎng)絡(luò)時(shí)間,需要的時(shí)間會比較長,個(gè)別電腦上可能會出現(xiàn)不兼容現(xiàn)象
        :param host_URL: 目標(biāo)網(wǎng)址,如:https://www.baidu.com/
        :param year_str: 年份中間的間隔字符,如:2019-11-22
        :param time_str: 小時(shí)和分鐘中將的間隔字符,如:12:30:59
        :return: 返回時(shí)間字符串,如:2019-11-22 12:30:59
        '''
        conn = http.client.HTTPConnection(host_URL)
        conn.request("GET", "/")
        r = conn.getresponse()
        # r.getheaders() #獲取所有的http頭
        ts = r.getheader('date')  # 獲取http頭date部分
        print(ts)
        # 將GMT時(shí)間轉(zhuǎn)換成北京時(shí)間
        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)
        year_out = "{}{}{:0>2}{}{:0>2}".format(ttime.tm_year, year_str, ttime.tm_mon, year_str, ttime.tm_mday)
        time_out = "{:0>2}{}{:0>2}{}{:0>2}".format(ttime.tm_hour, time_str, ttime.tm_min, time_str, ttime.tm_sec)
 
        output_str = year_out + "" + time_out
        print("目標(biāo)網(wǎng)址={} 的網(wǎng)絡(luò)時(shí)間={}".format(host_URL, output_str))
 
        print("return 時(shí)間={}".format(output_str))
        return output_str

調(diào)用方法

if __name__ == '__main__':
    test=Admin()
    test.get_web_server_time('www.baidu.com')

返回結(jié)果

目標(biāo)網(wǎng)址=www.baidu.com 的網(wǎng)絡(luò)時(shí)間=2022-01-11 19:58:02
return 時(shí)間=2022-01-11 19:58:02

方法二

獲取網(wǎng)絡(luò)時(shí)間,返回時(shí)間格式為毫秒:2019-12-13 11:39:48.398

代碼實(shí)現(xiàn)

def get_web_now_time(self, time_format='YYYY-MM-DD HH:mm:ss.SSS'):
        """
        獲取網(wǎng)絡(luò)時(shí)間,返回時(shí)間格式:2019-12-13 11:39:48.398
        :param time_format:控制返回字符串的格式,默認(rèn)為:'YYYY-MM-DD HH:mm:ss.SSS'
        :return: 
        """
        import arrow as ar
        import requests as req
        print('\n=========    獲取網(wǎng)絡(luò)時(shí)間   =========')
 
        try:
            res = req.get('https://www.baidu.com/').headers['Date']
            # res = req.get('https://www.hao123.com/').headers['Date']
            time_diff = ar.get(res[4:-4], 'DD MMM YYYY HH:mm:ss') - ar.now().floor('second')
            web_now_time = (ar.now() + time_diff).format(time_format)
 
            print('web_now_time={}'.format(web_now_time))
 
            return web_now_time
        except BaseException as e:
            print('獲取網(wǎng)絡(luò)時(shí)間出錯(cuò),出錯(cuò)原因:{}'.format(e))
            return -1

調(diào)用方法

if __name__ == '__main__':
    test=Admin()
    test.get_web_now_time()

返回結(jié)果

=========    獲取網(wǎng)絡(luò)時(shí)間   =========
web_now_time=2022-01-11 22:37:30.360

到此這篇關(guān)于Python獲取網(wǎng)絡(luò)時(shí)間戳的兩種方法詳解的文章就介紹到這了,更多相關(guān)Python獲取網(wǎng)絡(luò)時(shí)間戳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論