Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import subprocess
- from PyQt6 import QtWidgets, uic
- from PyQt6.QtCore import QObject, pyqtSignal, pyqtSlot, QThread
- class Action(QObject):
- runProcess = pyqtSignal(str)
- @pyqtSlot()
- def RunProcess(self):
- self.process = subprocess.Popen(["nmap\\ncat.exe", "-lvkp", "666"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
- creationflags=0x08000000, universal_newlines=True, encoding="utf8", errors="ignore")
- self.i = 0
- while (True):
- if self.process.poll() is None:
- output = self.process.stdout.readline()
- print(output) # for debugging
- prettyOutput = output
- window.chat_console.insertPlainText(prettyOutput)
- QtWidgets.QApplication.processEvents()
- else: # if the process is dead it breaks the loop
- break
- print(self.i) # debug
- self.i = self.i + 1
- class MainWindow(QtWidgets.QMainWindow):
- def Send(self):
- print("tried to write into stdin")
- self.action.process.stdin.writelines("test\n") # trying to write to stdin
- self.action.process.stdin.flush() # hoping that flush would write
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- uic.loadUi(f"gui.ui", self)
- self.show()
- self.action = Action()
- self.thread = QThread(self)
- self.action.runProcess.connect(Action.RunProcess)
- self.action.moveToThread(self.thread)
- self.thread.started.connect(self.action.RunProcess)
- self.thread.start()
- self.send_button.clicked.connect(lambda: self.Send())
- def closeEvent(self, event):
- y = self.action.i # saves the number of the current loop
- self.action.process.terminate() # kills the subprocess
- while(True): # make the program wait and not quit too early
- if self.action.i > y: # checks if the current loop number is higher than saved
- self.thread.terminate() # kills the thread if the loop stopped
- exit() # nicely ends the process
- app = QtWidgets.QApplication(sys.argv)
- window = MainWindow()
- app.exec()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement