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)文章
Python中Matplotlib圖像添加標(biāo)簽的方法實(shí)現(xiàn)
本文主要介紹了Python中Matplotlib圖像添加標(biāo)簽的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04python控制臺中實(shí)現(xiàn)進(jìn)度條功能
這篇文章主要介紹了python控制臺中實(shí)現(xiàn)進(jìn)度條功能的方法,想要了解的朋友可以參考一下2015-11-11利用Python爬蟲爬取金融期貨數(shù)據(jù)的案例分析
從技術(shù)角度來看,經(jīng)過一步步解析,任務(wù)是簡單的,入門requests爬蟲及入門pandas數(shù)據(jù)分析就可以完成,本文重點(diǎn)給大家介紹Python爬蟲爬取金融期貨數(shù)據(jù)的案例分析,感興趣的朋友一起看看吧2022-06-06python中的對數(shù)log函數(shù)表示及用法
在本篇文章里小編給大家整理了一篇關(guān)于python中的對數(shù)log函數(shù)表示及用法,有需要的朋友們可以學(xué)習(xí)下。2020-12-12django rest framework 數(shù)據(jù)的查找、過濾、排序的示例
這篇文章主要介紹了 django rest framework 數(shù)據(jù)的查找、過濾、排序,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06python利用后綴表達(dá)式實(shí)現(xiàn)計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了python利用后綴表達(dá)式實(shí)現(xiàn)計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-02-02python numpy函數(shù)中的linspace創(chuàng)建等差數(shù)列詳解
numpy.linspace是用于創(chuàng)建一個(gè)一維數(shù)組,并且是等差數(shù)列構(gòu)成的一維數(shù)組,下面這篇文章主要給大家介紹了關(guān)于python numpy函數(shù)中的linspace創(chuàng)建等差數(shù)列的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-10-10Python中json.dumps()函數(shù)的使用解析
json.dumps將一個(gè)Python數(shù)據(jù)結(jié)構(gòu)轉(zhuǎn)換為JSON,本文介紹了Python中json.dumps()函數(shù)的具體使用方法,以及和dump的區(qū)別,感興趣的可以了解一下2021-05-05