TypeError:缓冲区需要一个整数

时间:2015-10-19 11:19:20

标签: python-2.7 sockets ip

我正在尝试构建一个数据包嗅探器,但每次尝试运行此python我都会得到一个TypeError,当我试图读取数据包的前20个字节时,需要一个整数。

这是代码的一部分,它给了我一个错误:

try:
while True:

    #read in a packet
    raw_buffer = sniffer.recvfrom(65565)[0]

    #create an IP Header from the first 20 bytes of the buffer
    ip_header = IP(raw_buffer[0:20])

    #print out the protocol that was detected and the hosts
    print "Protocol: %s %s -> %s" % (ip_header.protocol, ip_header.src_address, ip_header.dst_address)

1 个答案:

答案 0 :(得分:0)

您假设IP标头是接收缓冲区的第一个,但可能不是。通常,以太网头位于其前面,因此可能导致IP构造函数失败。