简体   繁体   English

Python队列和线程混淆

[英]Python Queue and Threading confusion

I made a sniffer in Python which calculates the size of IP packets. 我用Python制作了一个嗅探器来计算IP数据包的大小。 I tried to get the size in two ways: 我试图通过两种方式获得尺寸:

1) simply by calculating len(pkt) 1)简单地通过计算len(pkt)

2) by extracting the packet length from the IP header and adding 14bytes for the ethernet header 2)从IP报头中提取数据包长度,并为以太网报头添加14字节

When comparing the result from len(pkt) with the extracted value from the IP header, they were almost always the same (ok, for very few packets there was a difference of 4-6 bytes but that's another question). 当将len(pkt)的结果与来自IP头的提取值进行比较时,它们几乎总是相同的(好的,对于非常少的数据包,存在4-6个字节的差异,但这是另一个问题)。

But once I implemented queues and threading into my code, the sizes from len(pkt) and the extracted value from the IP header are in most cases totally different. 但是一旦我实现了队列和线程到我的代码中,len(pkt)的大小和IP头中提取的值在大多数情况下完全不同。 Sometimes there is a difference of just few bytes, and sometimes several hundred bytes. 有时只有几个字节,有时几百个字节。 But very rarely they are the same. 但很少他们是一样的。

The code in which I implemented threading is below. 我实现线程的代码如下。 Does anybody have an idea if I made a mistake in the way I implemented threading/queues or what I am doing wrong? 如果我在实施线程/队列或我做错了的方式上犯了错误,有没有人知道?

import pcap
import struct
import dpkt
from Queue import Queue
from threading import Thread

def packet_handler():
    ts,pkt=q.get()
    eth=dpkt.ethernet.Ethernet(pkt)
    if eth.type != dpkt.ethernet.ETH_TYPE_IP:
        return
    a=struct.unpack('!BBHHHBBH4s4s', pkt[14:34])
    print a[2]+14,len(pkt)

def start():  
    pc.loop(0,lambda ts,pkt: q.put((ts,pkt)))

q=Queue()
pc=pcap.pcap(name="eth0")
start_sniffer=Thread(target=start)
start_sniffer.start()

while True:
    packet_handler()

Part of the output looks like this: 部分输出如下所示:

419 1454
419 419
54 60
389 60
389 389
405 60
405 405
405 60
405 405
54 60
54 60
493 491
491 492
491 493
491 491
502 502
54 60

I think I solved it. 我想我解决了。 I changed this line: 我更改了这一行:

pc.loop(0,lambda ts,pkt: q.put((str(ts),str(pkt))))

That means, I converted ts and pkt to strings before adding them to the queue. 这意味着,在将ts和pkt添加到队列之前,我将其转换为字符串。

Sizes for few packets are still different for 4-6 bytes, but that was also the case with the not-threading version. 少数数据包的大小对于4-6个字节仍然不同,但非线程版本的情况也是如此。 I will probably ask about that in a new post. 我可能会在新帖子中询问这个问题。

Thanks everybody for the help!!! 谢谢大家的帮助!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM