from graphics import *
from scapy.all import *
from collections import Counter
def main():
filename = str(raw_input("What is the name of the file? "))
# sets packet source IPAs to sources, sourcenum also has # of occurrences
IP.payload_guess = []
sources = list((p[IP].src) for p in PcapReader(filename) if IP in p)
sourcenum = collections.Counter(sources)
print sourcenum
def makegraph():
howmany = sum(1 for x in sourcenum.values())
width = 1000/howmany
# creates graph window with white background
win = GraphWin("Packets Sent From Certain Addresses", 1080, 360)
win.setBackground("white")
Line(Point(80, 330), Point(1080, 330)).draw(win)
Line(Point(80, 0), Point(80, 330)).draw(win)
# creates y axis labels
Text(Point(40, 330), " 0k pkts").draw(win)
Text(Point(40, 280), " 3k pkts").draw(win)
Text(Point(40, 230), " 6k pkts").draw(win)
Text(Point(40, 180), " 9k pkts").draw(win)
Text(Point(40, 130), " 12k pkts").draw(win)
Text(Point(40, 80), " 15k pkts").draw(win)
Text(Point(40, 30), " 18k+ pkts").draw(win)
# create text and bar for each IPA
a = 80
subaddr = 1
for ipa in sourcenum:
whooheight = sourcenum.get(str(ipa)) \t \t \t
hooheight = whooheight/(18000/292)
hoheight = 330-hooheight
print hoheight \t
if hoheight >= 30:
hoopyheight = hoheight
else:
hoopyheight = 30
bar = Rectangle(Point(a, 330), Point(a + width, hoopyheight))
bar.setFill("blue")
bar.draw(win)
Text(Point(a + width/2, 345), ipa).draw(win)
Text(Point(a + width/2, hoopyheight-15), str(whooheight) + " packets").draw(win)
a += width
raw_input("Press <Enter> to quit")
win.close()
makegraph()
if __name__ == "__main__":
main()
我建議你使用像[Pygal](http://pygal.org/)這樣的包來創建圖表。但是,也許這需要你太多 - 我不知道你對Python有多熟悉。 – Gandaro