使用Scapy将DNS数据包转换为字符串

时间:2014-08-21 17:15:06

标签: python dns scapy

我想将DNS数据包转换为DNS(数据包).show()等字符串。但是,show方法所做的只是打印出信息。我想将完全相同的信息存储在字符串中。有没有具体的方法来做到这一点?

1 个答案:

答案 0 :(得分:1)

以下代码段将有所帮助:

>>> from scapy.all import *
>>> import sys
>>> from cStringIO import StringIO
>>> old_stdout = sys.stdout
>>> sys.stdout = mystdout = StringIO()
>>> pkt = DNS()
>>> pkt.show()
>>> sys.stdout = old_stdout
>>> mystdout.getvalue()
'###[ DNS ]###\n  id        = 0\n  qr        = 0\n  opcode    = QUERY\n  aa        = 0\n  tc        = 0\n  rd        = 0\n  ra        = 0\n  z         = 0\n  rcode     = ok\n  qdcount   = 0\n  ancount   = 0\n  nscount   = 0\n  arcount   = 0\n  qd        = None\n  an        = None\n  ns        = None\n  ar        = None\n'
>>>