I'm trying to make my first ever program, a port scanner that shows all the open ports on a remote server, I've gotten it to work in CLI (thanks to the internet) but decided to make a GUI (Qt5) for it. I want textbox2 to output all the open ports after entering an IP address and clicking "Scan!", and obviously for the program to not crash after clicking it. Here is the relevant code to replicate the issue
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QPushButton, QAction, QLineEdit, QMessageBox, QPlainTextEdit, QVBoxLayout, QLabel
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot, Qt
import socket
import time
import sys
class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'PPort'
self.left = 10
self.top = 10
self.width = 800
self.height = 400
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.label = QLabel('Enter Target Address:', self)
self.label.move(50, -110)
self.label.resize(300, 300)
self.label2 = QLabel('Output:', self)
self.label2.move(50, 80)
self.label2.resize(300, 300)
self.textbox = QLineEdit(self)
self.textbox.move(50, 60)
self.textbox.resize(540, 30)
self.textbox2 = QPlainTextEdit(self)
self.textbox2.move(50, 250)
self.textbox2.resize(700, 100)
self.textbox2.setReadOnly(True)
self.button = QPushButton('Scan!', self)
self.button.move(620, 60)
self.button.clicked.connect(self.on_click)
self.show()
@pyqtSlot()
def on_click(self):
textboxValue = self.textbox.text()
socket.gethostbyname(textboxValue)
try:
for port in range(1, 1025):
socketprofile = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = socketprofile.connect_ex((textboxValue, port))
if result == 0:
self.textbox2.appendPlainText('Port {} Is open'.format(port))
socketprofile.close()
except socket.gaierror:
self.textbox2.appendPlainText('Hostname could not be resolved')
time.sleep(5)
sys.exit()
except socket.error:
self.textbox2.appendPlainText("Couldn't connect to server")
time.sleep(5)
sys.exit()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
No error is shown in textbox2, what is strange to me is that even when I replace self.textbox2.appendPlainText
with print(), it still doesn't output any error messages in the vscode terminal. However, entering an invalid IP address shows the gaierror(couldn't resolve host), and not in textbox2, but in the terminal, compared to how it always crashes if you enter a valid IP address(8.8.8.8, 192.168.0.1). I suspect I'm using if/for/try wrongly making it loop, but I can't really see what I'm doing wrong as I barely know what I'm doing as a newbie.