/
main.py
93 lines (61 loc) · 2.17 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'ipetrash'
# pip install pyqt5
from PyQt5 import Qt
# pip install scapy
from scapy.all import sniff
def log_uncaught_exceptions(ex_cls, ex, tb):
text = '{}: {}:\n'.format(ex_cls.__name__, ex)
import traceback
text += ''.join(traceback.format_tb(tb))
print(text)
Qt.QMessageBox.critical(None, 'Error', text)
quit()
import sys
sys.excepthook = log_uncaught_exceptions
class SniffThread(Qt.QThread):
about_new_data = Qt.pyqtSignal(str)
def _packethandler(self, pkt):
data = pkt.summary()
print(data)
self.about_new_data.emit(data)
def run(self):
sniff(filter="tcp", prn=self._packethandler)
class MainWindow(Qt.QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('Sniff with scapy')
self.lw_packet = Qt.QListWidget()
self.cb_log = Qt.QCheckBox('Write log')
self.cb_log.setChecked(True)
self.pb_clear = Qt.QPushButton('Clear')
self.pb_clear.clicked.connect(self.lw_packet.clear)
self.cb_autoscroll = Qt.QCheckBox('Autoscroll')
layout = Qt.QHBoxLayout()
layout.addWidget(self.cb_log)
layout.addWidget(self.cb_autoscroll)
layout.addStretch()
layout.addWidget(self.pb_clear)
main_layout = Qt.QVBoxLayout()
main_layout.addLayout(layout)
main_layout.addWidget(self.lw_packet)
central_widget = Qt.QWidget()
central_widget.setLayout(main_layout)
self.setCentralWidget(central_widget)
self.thread = SniffThread()
self.thread.about_new_data.connect(self._append_new_item)
self.thread.start()
def _append_new_item(self, data):
# Если флаг не стоит
if not self.cb_log.isChecked():
return
self.lw_packet.addItem(data)
# Если нужно автоматически прокручивать список вниз
if self.cb_autoscroll.isChecked():
self.lw_packet.scrollToBottom()
if __name__ == "__main__":
app = Qt.QApplication([])
mw = MainWindow()
mw.show()
app.exec_()