Python讀取Pickle文件信息并計(jì)算與當(dāng)前時(shí)間間隔的方法分析
本文實(shí)例講述了Python讀取Pickle文件信息并計(jì)算與當(dāng)前時(shí)間間隔的方法。分享給大家供大家參考,具體如下:
python—–讀取Pickle文件信息計(jì)算出與當(dāng)前的時(shí)間間隔
生成h_dic.pkl文件信息
root@kali:~/python/snmp# cat snmpserver.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
import datetime#導(dǎo)入時(shí)間戳
import SocketServer
import pickle
pfile = 'h_dic.pkl'#定義pickle文件,并生成h_dic.pkl文件
#讀取目錄下的celie.txt文件
host_status = {}#新建字典,使用IP地址作為KEY值。作用是來(lái)判斷每個(gè)客戶(hù)端IP多久與服務(wù)器通信一次的
f = open('celie.txt')#調(diào)用策略文檔,在里面的ip地址就可以通過(guò),并發(fā)送信息
while True:
line = f.readline().split()
if len(line) == 0:break
print line[0]#打印第一個(gè)IP地址信息
host_status[line[0]] = []#給字典第一個(gè)設(shè)置為空,這樣后面只要直接追加值就ok了
f.close()
class myMonitorHandler(SocketServer.BaseRequestHandler):
'''This is the Monitor server'''
def handle(self):
recv_data = self.request.recv(1024)#接收客戶(hù)端數(shù)據(jù)
if self.client_address[0] == '192.168.72.130':#如果IP為本機(jī)IP地址,就重新寫(xiě)入pickle文件信息
f2 = file(pfile,'w')#使用pickle模塊可寫(xiě)模式打開(kāi)文件f2
pickle.dump(host_status,f2)#使用pickle帶參數(shù)為字典名與文件名
f2.close()#關(guān)閉文件f2
if self.client_address[0] in host_status.keys():#如果存在字典中的ip地址信息,就返回對(duì)應(yīng)客戶(hù)端發(fā)送的Ip、時(shí)間戳、信息
#self.client_address為數(shù)組('192.168.72.129', 49109)的值。只要當(dāng)中的IP地址,因此取self.client_address[0]
#把host_status字典中的self.client_address[0]值即IP地址值賦值有兩個(gè)值,因此新建個(gè)列表,存取兩個(gè)值時(shí)間戳與接收的信息
#如:{'192.168.72.129': [(datetime.datetime(2017, 8, 20, 21, 29, 59, 415054), 'up')]}
#host_status[self.client_address[0]] = [(datetime.datetime.now(),recv_data)]
#直接把元組append進(jìn)字典
host_status[self.client_address[0]].append((datetime.datetime.now(),recv_data))
print 'From %s : %s %s' %(self.client_address,datetime.datetime.now(),recv_data)#打印客戶(hù)端地址、操作的時(shí)間戳值與接收的數(shù)據(jù)
#print host_status
else:#不存在字典中,則如下提示信息
print "sorry, ip %s is not in the monitor list" % self.client_address[0]
#打印出192.168.72.129 [(datetime.datetime(2017, 8, 20, 22, 1, 6, 705498), 'up')]
for t,m in host_status.items():
print t,m
if __name__ == "__main__":#當(dāng)自己運(yùn)行時(shí)調(diào)用什么什么;當(dāng)被其他程序調(diào)用時(shí)調(diào)用什么什么,如果被其他程序調(diào)用了,下面代碼不執(zhí)行
host,port = '',18000
server = SocketServer.ThreadingTCPServer((host,port),myMonitorHandler)#調(diào)用TCP的多線程
server.serve_forever()
root@kali:~/python/snmp#
pickle文件信息
root@kali:~/python/snmp# ls celie.txt h_dic.pkl m_handle.py snmpclient2.py snmpserver.py tab.py tab.pyc root@kali:~/python/snmp# cat h_dic.pkl (dp0 S'192.168.72.129' p1 (lp2 (cdatetime datetime p3 (S'\x07\xe1\x08\x16\x149\x1b\x02\xd0F' p4 tp5 Rp6 S'up' p7 tp8 a(g3 (S'\x07\xe1\x08\x16\x149#\x03\xeag' p9 tp10 Rp11 S'up' p12 tp13 a(g3 (S'\x07\xe1\x08\x16\x149*\x01Fd' p14 tp15 Rp16 S'up' p17 tp18 a(g3 (S"\x07\xe1\x08\x16\x14:'\x06\x9di" p19 tp20 Rp21 S'up' p22 tp23 a(g3 (S'\x07\xe1\x08\x16\x15\x0c\x16\x00=\x9f' p24 tp25 Rp26 S'up' p27 tp28 a(g3 (S'\x07\xe1\x08\x16\x15\x0c\x16\te\x8c' p29 tp30 Rp31 S'up' p32 tp33 as.root@kali:~/python/snmp#
調(diào)用h_dic.pkl文件m_handle.py的代碼
運(yùn)行情況1
root@kali:~/python/snmp# cat m_handle.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
from datetime import datetime#導(dǎo)入時(shí)間模塊
import pickle#導(dǎo)入pickle模塊
f = file('h_dic.pkl','rb')#使用rb讀取模式打開(kāi)pickle文件
host_status = pickle.load(f)#使用pickle的load方式打開(kāi)文件,變成字典
for h,m in host_status.items():#在字典中循環(huán)元素
if len(m) != 0:#如果時(shí)間值不為空,則進(jìn)入
print h,m[-1]
root@kali:~/python/snmp# python m_handle.py
192.168.72.129 (datetime.datetime(2017, 8, 22, 21, 12, 22, 615820), 'up')
root@kali:~/python/snmp#
運(yùn)行情況2
root@kali:~/python/snmp# cat m_handle.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
from datetime import datetime#導(dǎo)入時(shí)間模塊
import pickle#導(dǎo)入pickle模塊
f = file('h_dic.pkl','rb')#使用rb讀取模式打開(kāi)pickle文件
host_status = pickle.load(f)#使用pickle的load方式打開(kāi)文件,變成字典
for h,m in host_status.items():#在字典中循環(huán)元素
if len(m) != 0:#如果時(shí)間值不為空,則進(jìn)入
old_time = m[-1][0]#取時(shí)間值出來(lái)
print h,(datetime.now() - old_time).seconds#打印主機(jī)ip,當(dāng)前時(shí)間減去從pickle文件中讀取的值
root@kali:~/python/snmp#
運(yùn)行情況
root@kali:~/python/snmp# python m_handle.py 192.168.72.129 1007 root@kali:~/python/snmp#
運(yùn)行情況2
root@kali:~/python/snmp# cat m_handle.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
from datetime import datetime#導(dǎo)入時(shí)間模塊
import pickle#導(dǎo)入pickle模塊
f = file('h_dic.pkl','rb')#使用rb讀取模式打開(kāi)pickle文件
host_status = pickle.load(f)#使用pickle的load方式打開(kāi)文件,變成字典
for h,m in host_status.items():#在字典中循環(huán)元素
if len(m) != 0:#如果時(shí)間值不為空,則進(jìn)入
old_time = m[-1][0]#取時(shí)間值出來(lái)
time_diff = (datetime.now() - old_time).seconds#當(dāng)前時(shí)間減去從pickle文件中讀取的值,為時(shí)間差值
if time_diff > 30:#如果時(shí)間差大于30秒,則進(jìn)入
print 'No data received from %s for %s ,please check!' %(h,time_diff)
else:
print h,(datetime.now() - old_time).seconds#打印主機(jī)ip,時(shí)間差值
root@kali:~/python/snmp#
root@kali:~/python/snmp# python m_handle.py
No data received from 192.168.72.129 for 494 ,please check!
root@kali:~/python/snmp#
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Python文件與目錄操作技巧匯總》、《Python文本文件操作技巧匯總》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門(mén)與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
用python實(shí)現(xiàn)的可以拷貝或剪切一個(gè)文件列表中的所有文件
python 實(shí)現(xiàn)剪切或是拷貝一個(gè)文件列表中的所有文件2009-04-04
conda創(chuàng)建pytorch環(huán)境報(bào)錯(cuò)
這篇文章主要介紹了conda創(chuàng)建pytorch環(huán)境報(bào)錯(cuò),幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-04-04
python spilt()分隔字符串的實(shí)現(xiàn)示例
split() 方法可以實(shí)現(xiàn)將一個(gè)字符串按照指定的分隔符切分成多個(gè)子串,本文介紹了spilt的具體使用,感興趣的可以了解一下2021-05-05
Python中數(shù)據(jù)清洗與處理的常用方法小結(jié)
數(shù)據(jù)清洗與處理是數(shù)據(jù)分析的重要步驟,確保數(shù)據(jù)的準(zhǔn)確性和一致性,這篇文章為大家整理了Python中一些常用的數(shù)據(jù)清洗與處理方法,需要的可以參考下2025-02-02
利用Python函數(shù)實(shí)現(xiàn)一個(gè)萬(wàn)歷表完整示例
這篇文章主要給大家介紹了關(guān)于如何利用Python函數(shù)實(shí)現(xiàn)一個(gè)萬(wàn)歷表的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Python實(shí)現(xiàn)高效求解素?cái)?shù)代碼實(shí)例
這篇文章主要介紹了Python實(shí)現(xiàn)高效求解素?cái)?shù)代碼實(shí)例,本文直接給出代碼實(shí)例,需要的朋友可以參考下2015-06-06
Python Pytorch深度學(xué)習(xí)之圖像分類(lèi)器
今天小編就為大家分享一篇關(guān)于Pytorch圖像分類(lèi)器的文章,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-10-10
python 使用正則表達(dá)式按照多個(gè)空格分割字符的實(shí)例
今天小編就為大家分享一篇python 使用正則表達(dá)式按照多個(gè)空格分割字符的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12

