我在將 Tkinter GUI 中的輸入項匯入主程式時遇到問題,需要幫助解決問題。兩個代碼如下。首先是到目前為止的 GUI 代碼。
# GUI Code
import os
import platform
import tkinter as tk
import threading
def run():
if platform.system() == "Windows":
os.system('cmd /c PortScanFinal.py')
if platform.system() == "Linux":
os.system("python3 ./PortScanFinal.py")
def open_firewall():
if platform.system() == 'Windows':
os.system("cmd /c control")
if platform.system() == 'Linux':
os.system("sudo iptables -L -v -n | more")
root = tk.Tk()
root.title("Simple Port Scanner")
root.geometry("450x300")
val1 = tk.StringVar()
val2 = tk.StringVar()
lbl = tk.Label(root, text="Enter the IP address/web address: ")
txt = tk.Entry(root, textvariable=val1, width=15)
txt.focus()
lbl2 = tk.Label(root, text="Please enter file path for Port Scan results: ")
txt2 = tk.Entry(root, textvariable=val2, width=20)
txt2.focus()
txt2_run = tk.Button(root, text="Run Port Scan", command=threading.Thread(target=run).start)
btn1 = tk.Button(root, text="Start Firewall Editing", command=open_firewall)
btn2 = tk.Button(root, text="Quit", command=root.quit)
lbl.pack()
txt.pack()
lbl2.pack()
txt2.pack()
txt2_run.pack()
btn1.pack()
btn2.pack()
if __name__ == "__main__":
root.mainloop()
接下來是主要的python代碼
#Port Scanner Code
import sys
import time
from socket import *
import PortScannerGUI as Gui
startTime = time.time()
portlist = [7, 19, 20, 21, 22, 23, 25, 37, 53, 69, 79, 80, 110, 111, 135, 137, 138, 139, 161, 443, 445, 512, 513, 514,
1433, 1434, 1723, 3389, 8080]
NecessaryPorts = [20, 21, 25, 53, 80, 123, 135, 139, 143, 161, 443, 445]
OpenPorts = []
# target = input("Enter a remote host address:")
try:
ipaddr = Gui.txt.get()
target = ipaddr # input("Enter remote IP/Web address here: ")
IPget = gethostbyname(target)
print('Starting scan on host:', IPget)
for i in portlist:
s = socket(AF_INET, SOCK_STREAM)
conn = s.connect_ex((IPget, i))
if conn == 0:
OpenPorts.append(i)
print('Port %d: OPEN' % (i,))
s.close()
else:
# ClosedPorts.append(i)
print('Port %d: CLOSED' % (i,))
s.close()
print('Time tasken:', time.time() - startTime)
print('Results being printed to file within program execution folder')
# Sets output path for results of scan, Please replace file path when using for own purposes.
out_path = Gui.txt2.get()
sys.stdout = open(out_path, 'w ') # Opens file in path with write capabilities
print("Please note that the following ports are generally necessary for computer function, apply firewall rules \n"
"that do not fully restrict the access to these ports:", NecessaryPorts)
print("Ports that are open:", OpenPorts)
print("Use the Open Command Center button on the Port Scanner GUI to begin navigation to the Windows Firewall Settings")
sys.stdout.close()
except gaierror as e:
print("Invalid IP address or web address used, please enter a proper address. \nEx: 127.0.0.1 or Google.com \nPlease try again.")
有人可以幫我弄清楚出了什么問題嗎?當我測驗 GUI 和主代碼時,它只會在放置用戶輸入之前捕獲空白輸入行。
*Edit-------- 當從 GUI 中洗掉 get 函式并在埠掃描器中呼叫它時,我仍然得到以下結果。
Starting scan on host: 0.0.0.0
Port 7: CLOSED
Port 19: CLOSED
Port 20: CLOSED
Port 21: CLOSED
Port 22: CLOSED
Port 23: CLOSED
Port 25: CLOSED
Port 37: CLOSED
Port 53: CLOSED
Port 69: CLOSED
Port 79: CLOSED
Port 80: CLOSED
Port 110: CLOSED
Port 111: CLOSED
Port 135: CLOSED
Port 137: CLOSED
Port 138: CLOSED
Port 139: CLOSED
Port 161: CLOSED
Port 443: CLOSED
Port 445: CLOSED
Port 512: CLOSED
Port 513: CLOSED
Port 514: CLOSED
Port 1433: CLOSED
Port 1434: CLOSED
Port 1723: CLOSED
Port 3389: CLOSED
Port 8080: CLOSED
Time tasken: 0.006447315216064453
Results being printed to file within program execution folder
uj5u.com熱心網友回復:
查看原始代碼的注釋和代碼更改。
PortScannerGUI.py
# File: PortScannerGUI.py
# GUI Code
import os
import platform
import tkinter as tk
import threading
from PortScanFinal import port_scan # Import the function version of
# previous code.
def run(address, file_out): # Add address parameter to this function.
if platform.system() == "Windows":
port_scan(address, file_out)
if platform.system() == "Linux":
os.system("python3 ./PortScanFinal.py")
def open_firewall():
if platform.system() == 'Windows':
os.system("cmd /c control")
if platform.system() == 'Linux':
os.system("sudo iptables -L -v -n | more")
root = tk.Tk()
root.title("Simple Port Scanner")
root.geometry("450x300")
val1 = tk.Variable()
val2 = tk.Variable()
lbl = tk.Label(root, text="Enter the IP address/web address: ")
txt = tk.Entry(root, textvariable=val1, width=15)
txt.focus()
lbl2 = tk.Label(root, text="Please enter file path for Port Scan results: ")
txt2 = tk.Entry(root, textvariable=val2, width=20)
txt2.focus()
# Use a lambda function to call the run function with a parameter.
# txt.get()
txt2_run = tk.Button(root, text="Run Port Scan",
command=lambda: threading.Thread(target=run(txt.get(
), txt2.get())).start)
btn1 = tk.Button(root, text="Start Firewall Editing", command=open_firewall)
btn2 = tk.Button(root, text="Quit", command=root.quit)
ipaddr = txt.get()
path = txt2.get()
lbl.pack()
txt.pack()
lbl2.pack()
txt2.pack()
txt2_run.pack()
btn1.pack()
btn2.pack()
if __name__ == "__main__":
root.mainloop()
PortScanFinal.py
# File: PortScanFinal.py
# Import this file into PortScannerGUI
# Port Scanner Code
import sys
import time
from socket import *
# import PortScannerGUI as Gui # Remove import here.
startTime = time.time()
portlist = [7, 19, 20, 21, 22, 23, 25, 37, 53, 69, 79, 80, 110, 111, 135, 137,
138, 139, 161, 443, 445, 512, 513, 514,
1433, 1434, 1723, 3389, 8080]
NecessaryPorts = [20, 21, 25, 53, 80, 123, 135, 139, 143, 161, 443, 445]
OpenPorts = []
# target = input("Enter a remote host address:")
# Make the following code a function.
def port_scan(target, out_path):
try:
# target = Gui.txt.get() # input("Enter remote IP/Web address here: ")
IPget = gethostbyname(target)
print('Starting scan on host:', IPget)
for i in portlist:
s = socket(AF_INET, SOCK_STREAM)
conn = s.connect_ex((IPget, i))
if conn == 0:
OpenPorts.append(i)
print('Port %d: OPEN' % (i,))
s.close()
else:
# ClosedPorts.append(i)
print('Port %d: CLOSED' % (i,))
s.close()
print('Time tasken:', time.time() - startTime)
print('Results being printed to file within program execution folder')
# Sets output path for results of scan, Please replace file path when using for own purposes.
# out_path = Gui.txt2.get()
sys.stdout = open(out_path, 'w ') # Opens file in path with write
# capabilities
print(
"Please note that the following ports are generally necessary for computer function, apply firewall rules \n"
"that do not fully restrict the access to these ports:",
NecessaryPorts)
print("Ports that are open:", OpenPorts)
print(
"Use the Open Command Center button on the Port Scanner GUI to begin navigation to the Windows Firewall Settings")
sys.stdout.close()
except gaierror as e:
print(
"Invalid IP address or web address used, please enter a proper address. \nEx: 127.0.0.1 or Google.com \nPlease try again.")
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/497230.html
下一篇:如何添加串列框?
