首页 > 编程 > Python > 正文

Python读取Pickle文件信息并计算与当前时间间隔的方法分析

2020-02-16 00:59:50
字体:
来源:转载
供稿:网友

本文实例讲述了Python读取Pickle文件信息并计算与当前时间间隔的方法。分享给大家供大家参考,具体如下:

python—–读取Pickle文件信息计算出与当前的时间间隔

生成h_dic.pkl文件信息

root@kali:~/python/snmp# cat snmpserver.py#!/usr/bin/python# --*-- coding:utf-8 --*--import datetime#导入时间戳import SocketServerimport picklepfile = 'h_dic.pkl'#定义pickle文件,并生成h_dic.pkl文件#读取目录下的celie.txt文件host_status = {}#新建字典,使用IP地址作为KEY值。作用是来判断每个客户端IP多久与服务器通信一次的f = open('celie.txt')#调用策略文档,在里面的ip地址就可以通过,并发送信息while True:  line = f.readline().split()  if len(line) == 0:break  print line[0]#打印第一个IP地址信息  host_status[line[0]] = []#给字典第一个设置为空,这样后面只要直接追加值就ok了f.close()class myMonitorHandler(SocketServer.BaseRequestHandler):  '''This is the Monitor server'''  def handle(self):    recv_data = self.request.recv(1024)#接收客户端数据    if self.client_address[0] == '192.168.72.130':#如果IP为本机IP地址,就重新写入pickle文件信息      f2 = file(pfile,'w')#使用pickle模块可写模式打开文件f2      pickle.dump(host_status,f2)#使用pickle带参数为字典名与文件名      f2.close()#关闭文件f2    if self.client_address[0] in host_status.keys():#如果存在字典中的ip地址信息,就返回对应客户端发送的Ip、时间戳、信息      #self.client_address为数组('192.168.72.129', 49109)的值。只要当中的IP地址,因此取self.client_address[0]      #把host_status字典中的self.client_address[0]值即IP地址值赋值有两个值,因此新建个列表,存取两个值时间戳与接收的信息      #如:{'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进字典      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)#打印客户端地址、操作的时间戳值与接收的数据      #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,mif __name__ == "__main__":#当自己运行时调用什么什么;当被其他程序调用时调用什么什么,如果被其他程序调用了,下面代码不执行  host,port = '',18000  server = SocketServer.ThreadingTCPServer((host,port),myMonitorHandler)#调用TCP的多线程  server.serve_forever()root@kali:~/python/snmp#

pickle文件信息

root@kali:~/python/snmp# lscelie.txt h_dic.pkl m_handle.py snmpclient2.py snmpserver.py tab.py tab.pycroot@kali:~/python/snmp# cat h_dic.pkl(dp0S'192.168.72.129'p1(lp2(cdatetimedatetimep3(S'/x07/xe1/x08/x16/x149/x1b/x02/xd0F'p4tp5Rp6S'up'p7tp8a(g3(S'/x07/xe1/x08/x16/x149#/x03/xeag'p9tp10Rp11S'up'p12tp13a(g3(S'/x07/xe1/x08/x16/x149*/x01Fd'p14tp15Rp16S'up'p17tp18a(g3(S"/x07/xe1/x08/x16/x14:'/x06/x9di"p19tp20Rp21S'up'p22tp23a(g3(S'/x07/xe1/x08/x16/x15/x0c/x16/x00=/x9f'p24tp25Rp26S'up'p27tp28a(g3(S'/x07/xe1/x08/x16/x15/x0c/x16/te/x8c'p29tp30Rp31S'up'p32tp33as.root@kali:~/python/snmp#            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表