在Python中将输出内容打印到CSV文件中

3
我的代码用于将pcap文件中的IP地址打印在CSV文件中,它可以正常工作,但问题是它只存储了pcap文件的第一个数据包。我不知道出了什么问题... 请问有人能帮我解决这个问题吗?
以下是我的代码:
import dpkt
from dpkt.ip import IP
from dpkt.ethernet import Ethernet
import struct
import socket
import csv

def ip_to_str(address):
    return socket.inet_ntoa(address)

f = open('sample.pcap', 'rb')
pcap = dpkt.pcap.Reader(f)
for ts, buf in pcap:
    eth = dpkt.ethernet.Ethernet(buf)
    if eth.type != dpkt.ethernet.ETH_TYPE_IP:
        continue

    ip = eth.data
    do_not_fragment = bool(dpkt.ip.IP_DF)
    more_fragments = bool(dpkt.ip.IP_MF)
    fragment_offset = bool(dpkt.ip.IP_OFFMASK)
    c = csv.writer(open("a.csv", "wb"))

    Source = "%s" % ip_to_str(ip.src)
    Destination = "%s" % ip_to_str(ip.dst)
    Length = "%d" % (ip.len)
    TTL = "%d" % (ip.ttl)
    OFF = ip.off
    TOS = ip.tos
    Protocol = ip.p
    data = (Source, Destination, Length, TTL, TOS, OFF, Protocol)
    c.writerow(data)

请修复您代码中的缩进。 - Burhan Khalid
@BurhanKhalid,缩进没有错误。它工作得很好,但问题在于它只打印pcap的第一个数据包。 - Sheena Wadhwa
2个回答

0

你的代码目前在循环内部打开一个csv文件,因此每次都会创建一个新版本的"a.csv"并且只向其中写入一个数据包。将文件创建语句移到循环外部,并保持循环内部写入。

import dpkt
from dpkt.ip import IP
from dpkt.ethernet import Ethernet
import struct
import socket
import csv

def ip_to_str(address):
    return socket.inet_ntoa(address)

f = open('sample.pcap', 'rb')
pcap = dpkt.pcap.Reader(f)
c = csv.writer(open("a.csv", "wb"))  # <=== moved here
for ts, buf in pcap:
    eth = dpkt.ethernet.Ethernet(buf)
    if eth.type != dpkt.ethernet.ETH_TYPE_IP:
        continue

    ip = eth.data
    do_not_fragment = bool(dpkt.ip.IP_DF)
    more_fragments = bool(dpkt.ip.IP_MF)
    fragment_offset = bool(dpkt.ip.IP_OFFMASK)

    Source = "%s" % ip_to_str(ip.src)
    Destination = "%s" % ip_to_str(ip.dst)
    Length = "%d" % (ip.len)
    TTL = "%d" % (ip.ttl)
    OFF = ip.off
    TOS = ip.tos
    Protocol = ip.p
    data = (Source, Destination, Length, TTL, TOS, OFF, Protocol)
    c.writerow(data)

谢谢您的关注。但是当我在我的代码中使用您的编辑时,它甚至没有将单个数据包写入CSV文件。我不知道为什么。您能帮忙吗? - Sheena Wadhwa
你需要在循环结束时关闭文件。 - Burhan Khalid
你能再检查一下吗?对我来说它是正常工作的。我在这上面尝试了一个来自网络的样本pcap文件,csv被创建并填充整个pcap文件都很好。也许你可以在你的代码中添加一些打印来确保循环实际上正在循环:-) @Burhan Khalid 我认为在Python中,您可以不必显式关闭文件。 - sal
@sal,我已经检查了你的编辑,但对我来说不起作用,我不知道问题出在哪里 :( - Sheena Wadhwa
太好了!我已经没有其他想法了,不知道还有什么问题。 :-) - sal
显示剩余9条评论

0
你需要确保在循环结束时关闭文件,并且按照正确的缩进方式编写代码。
import struct
import socket
import csv

import dpkt

from dpkt.ip import IP
from dpkt.ethernet import Ethernet

def ip_to_str(address):
    return socket.inet_ntoa(address)

with open('sample.pcap', 'rb') as f:
    pcap = dpkt.pcap.Reader(f)

results = []
for ts, buf in pcap:
    eth = dpkt.ethernet.Ethernet(buf)
    if eth.type != dpkt.ethernet.ETH_TYPE_IP:
        continue

    ip = eth.data
    do_not_fragment = bool(dpkt.ip.IP_DF)
    more_fragments = bool(dpkt.ip.IP_MF)
    fragment_offset = bool(dpkt.ip.IP_OFFMASK)

    data = [ip_to_str(ip.src),
            ip_to_str(ip.dst),
            ip.len,
            ip.ttl,
            ip.off,
            ip.tos,
            ip.p]
    results.append(data)

with open('output.csv', 'wb') as f:
    writer = csv.writer(f, delimiter=',', quotechar='"')
    writer.writerows(results)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接