实习期工作总结python⽹络监控程序_python写的⼀个监控系统进程⽹络流量
的程序
python写的⼀个监控系统进程⽹络流量的程序,python2.7版本。 代码如下:
import socket
import struct
import thread
import threading
import time
import os
net_data = {}
d_net_info = {}
lock = threading.Lock()
def print_data():
while True:
lock.acquire()
for key in net_data:
print "%s %s\n"%(key, net_data[key])
股票收盘
time.sleep(5);
def get_net_info():
net_info = os.popen('netstat -nbo').readlines()
for l in net_info[4:]:
s = l.split()
if len(s)>2:
key = "%s %s"%(s[1],s[2])
key2 = "%s %s"%(s[2],s[1])
el:
if not d_net_info.has_key(key):
d_net_info[key] = s[0]
d_net_info[key2] = s[0]
实习工作
def get_packet():
HOST = hostname())
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
s.bind((HOST, 0))
s.tsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
丧气是什么意思net_data["unknow"] = 0
while True:
buf = s.recvfrom(65565)
port = struct.unpack('HH', buf[0][20:24])
src_ip = "%d.%d.%d.%d"%struct.unpack('BBBB', buf[0][12:16])
dest_ip ="%d.%d.%d.%d"%struct.unpack('BBBB', buf[0][16:20])
src_port = socket.htons(port[0])
纸箱制作
dest_port = socket.htons(port[1])
data_len = len(buf[0])
key="%s:%d %s:%d"%(src_ip,src_port,dest_ip,dest_port)
if not d_net_info.has_key(key):
get_net_info()
if d_net_info.has_key(key):
key2 ="%s %s"%(key,d_net_info[key])
双节if net_data.has_key(key2):
net_data[key2] =net_data[key2]+data_len
el:
net_data[key2] = data_len
el:云水禅心
net_data["unknow"] =net_data["unknow"] + data_len
thread.start_new_thread(print_data,())
get_packet()
自制钥匙扣
初学python,代码⽐较丑陋,可能效率和安全、稳定性⽅⾯也有很⼤问题,仅供初学者参考。