P
P
pypyshka2018-12-04 17:48:20
Python
pypyshka, 2018-12-04 17:48:20

The data is "cut off" when the sniffer is running. How to fix?

Hello!
I am learning to work with Python 3, trying to get data from a network device using a sniffer:

import re
from PyQt5 import QtCore
from scapy.all import *

class TestSniffer(QtCore.QThread):
    def __init__(self, data, parent=None):
        QtCore.QThread.__init__(self, parent)
        self.data = data

    def run(self):
        sniff(prn=self.pkt_callback, filter="tcp", store=0)
    
    def pkt_callback(self, pkt):
        # Фильтруем пакеты по мак-адресу
        if pkt.src == "мак_адрес_устройства": 
            if pkt.getlayer(Raw):
                data_dirty = pkt[Raw].load
                try:
                    # "Расшифровываем" данные пакета
                    data_decoded = data_dirty.decode("utf-8")
                    print(data_decoded)
                    # Ищем текст между тэгами
                    check_data = re.findall("<text .*>(.*?)</text>", data_decoded)
                    for line in check_data:
                        if "Test" not in line:
                            self.data.append(line)
                    print(self.data)
                    if len(self.data) == 6:
                        print("Good")
                        self.data = []
                except Exception as error:
                    print("Error: ", error)

While parsing packets, I keep getting the following data:
print(data_decoded):
<a id="19">
  <text font-family="monospace" font-size="10">001</text>
  <text font-family="monospace" font-size="10">Test</text>
</a>
<a id="20">
  <text font-family="monospace" font-size="10">002</text>
  <text font-family="monospace" font-size="10">Test</text>
</a>
<a id="21">
  <text font-family="monospace" font-size="10">003</text>
  <text font-family="monospace" font-size="10">Test</text>
</a>
<a id="22">
  <text font-family="monospace" font-size="10">0

print(self.data):
["001", "002", "003"]
print(data_decoded):
04</text>
  <text font-family="monospace" font-size="10">Test</text>
</a>
<a id="23">
  <text font-family="monospace" font-size="10">005</text>
  <text font-family="monospace" font-size="10">Test</text>
</a>
<a id="24">
  <text font-family="monospace" font-size="10">006</text>
  <text font-family="monospace" font-size="10">Test</text>
</a>

print(self.data):
["001", "002", "003", "005", "006"]
And so on... As you can see, part of the string was split and, accordingly, the regular expression could not process it and we received incomplete data. Is it possible to somehow "combine" the packages or maybe some other options? I would be very grateful for a hint where to dig =)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question