1
\$\begingroup\$

I'm new in python and I want to know the way to improve this code with best practices.

I'm using PyQT5 to create new thread In that thread I'm using scapy to sniff traffic and filter ARP traffic to find devices which are broadcasting the network.

In the complete code I'm filtering IP traffic too, but now I'm working on threads in order that GUI doesn't get freeze.

Here the code:

from PyQt5 import QtGui
from PyQt5.QtWidgets import * # QApplication, QDialog, QProgressBar, QPushButton, QVBoxLayout
import sys
from PyQt5.QtCore import Qt, QThread, pyqtSignal, QRect
from scapy.all import sniff, getmacbyip, ARP, get_if_addr, sr
from time import time, sleep
def scan_network(ProgressBar, sec):
 dict_scan = {}
 start_time = time()
 print (dict_scan)
 
def packet_deploy(change_value: pyqtSignal, start_time: float, dict_scan: dict, sec: int):
 def upload_packet(packet):
 if packet.haslayer("ARP"):
 # upload packet, using passed arguments
 dict_scan[(packet[ARP].hwsrc).replace(":", "").upper()] = packet[ARP].psrc
 LoadingBar(change_value, start_time, sec)
 return upload_packet
 
def LoadingBar(change_value, start_time, sec):
 ellapsed = int(((time() - start_time)*100) / sec)
 change_value.emit(ellapsed)
 
class MyThread(QThread):
 def __init__(self, myvar, parent = None):
 super(MyThread,self).__init__(parent)
 self.myvar = myvar[0]
 
 change_value = pyqtSignal(int)
 def run(self):
 seconds = self.myvar.txt_From.toPlainText()
 print (seconds)
 sec = 10
 dict_scan = {}
 start_time = time()
 sniff(prn=packet_deploy(self.change_value, start_time, dict_scan, sec), timeout = (sec), store=0)
 print (dict_scan)
 
class Window(QDialog):
 def __init__(self):
 super().__init__()
 self.title = "PyQt5 ProgressBar"
 self.top = 200
 self.left = 500
 self.width = 300
 self.height = 200
 self.setWindowIcon(QtGui.QIcon("icon.png"))
 self.setWindowTitle(self.title)
 self.setGeometry(self.left, self.top, self.width, self.height)
 vbox = QVBoxLayout()
 self.progressbar = QProgressBar()
 self.progressbar.setMaximum(100)
 self.progressbar.setStyleSheet("QProgressBar {border: 2px solid grey;border-radius:8px;padding:1px}"
 "QProgressBar::chunk {background:yellow}")
 
 vbox.addWidget(self.progressbar)
 self.button = QPushButton("Start Progressbar")
 self.button.clicked.connect(self.startProgressBar)
 self.button.setStyleSheet('background-color:yellow')
 
 
 self.txt_From = QTextEdit()
 self.txt_From.setObjectName(u"txt_From")
 self.txt_From.setGeometry(QRect(0, 0, 151, 21))
 vbox.addWidget(self.txt_From)
 
 vbox.addWidget(self.button)
 self.setLayout(vbox)
 self.show()
 
 def startProgressBar(self):
 self.thread = MyThread(myvar=[self])
 self.thread.change_value.connect(self.setProgressVal)
 self.thread.start()
 
 def setProgressVal(self, val):
 self.progressbar.setValue(val)
 
 
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())
asked Jul 4, 2021 at 7:01
\$\endgroup\$
1
  • \$\begingroup\$ Welcome to the Code Review Community. The title of the question should indicate what the code does not what your concerns about the code are. Please read How do I ask a good question. As part of a good code review we will sometimes mention best practices, but asking specifically about best practices will sometimes be considered off-topic. I do hope you get some good answers. \$\endgroup\$ Commented Jul 4, 2021 at 13:02

1 Answer 1

1
\$\begingroup\$

If your GUI is freezing then you are working your CPU or Memory too much.

I don't know intended usage of the App, but it might be worth considering splitting this script into two:

  1. First script will collect data.
  2. Second script will displays data.

Data collection can be handled in a separate thread or run on a specific core. Data can also be written into a file or some other data store. The display script will only focus on reading & displaying data and will have sufficient resources not to lag.

answered Jul 4, 2021 at 21:09
\$\endgroup\$
1
  • \$\begingroup\$ In this case I find the solution, it was a thread problem. When you work with PyQT5 you need to use qthread to avoid GUI freezing \$\endgroup\$ Commented Jul 7, 2021 at 21:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.