以csv文件python打印输出
我的代码用于在CSV文件中打印pcap文件IP的输出,工作正常,但问题是它只存储pcap文件的第一个数据包。我不明白真正的问题在哪里。。 那么有人能帮我解决这个问题吗 这是我的密码:以csv文件python打印输出,python,csv,Python,Csv,我的代码用于在CSV文件中打印pcap文件IP的输出,工作正常,但问题是它只存储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_n
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)
您的代码当前正在循环中打开一个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)
导入dpkt
从dpkt.ip导入ip
从dpkt.ethernet导入以太网
导入结构
导入套接字
导入csv
def ip_至_str(地址):
返回socket.inet\u ntoa(地址)
f=open('sample.pcap','rb')
pcap=dpkt.pcap.Reader(f)
c=csv.writer(打开(“a.csv”、“wb”)#您需要确保在循环结束时关闭文件;如前所述,正确缩进代码:
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)
修复代码中的缩进。@BurhanKhalid没有缩进的错误。它运行良好,但有一个问题,它只打印第一包PCAP感谢您的兴趣。但当我在代码中使用你的编辑时。它甚至不会将单个数据包写入csv。我不知道为什么。你能帮忙吗?你需要在循环结束时关闭文件。你能再检查一下吗?它正在为我工作。我尝试了一个来自web的示例pcap,整个pcap的csv创建和填充都很好。也许你可以在代码中添加一些打印,以确保循环实际上是。。。循环:-)@Burhan Khalid我认为在python中,你不必显式关闭文件就可以逃脱。@sal我已经检查了你的编辑,它对我不起作用,我不知道问题出在哪里:(还有几个问题要问你:python2或python3?有没有一种方法可以共享pcap?你能尝试一下吗:注释c.writerow(数据)
行并放置一个打印数据
语句,然后查看是否在屏幕上打印所有(或任何)数据包?