我的代码不会显示包含所有已打开端口的opened_ports列表。 (我认为它甚至不会为它增加价值。(也许覆盖?)) 我已经尝试了一些东西,但没有任何东西可以工作。
BTW:有没有办法对"端口x关闭进行排序。"?
输出:
...
Port 97 is closed.
Port 100 is closed.
All opened ports within the selected range:
[]
代码:
import socket, threading, time
from queue import Queue
print_lock = threading.Lock()
target = input('Target:' + ' ')
workers = input('Workers:' + ' ')
first_port = input('First port:' + ' ')
last_port = input('Last port:' + ' ')
if first_port == 'min':
first_port = 1
if last_port == 'max':
last_port = 65536
print('\n' + 'Scanning...' + '\n')
def scan(port):
soccer = socket.socket(socket.AF_INET, socket. SOCK_STREAM)
try:
connection = soccer.connect((target, port))
with print_lock:
print('Port' + ' ' + str(port) + ' ' + 'is opened.')
time.sleep(5)
opened_ports = opened_ports + port
connection.close()
except:
with print_lock:
print('Port' + ' ' + str(port) + ' ' + 'is closed.')
def thread():
while True:
worker = queue.get()
scan(worker)
queue.task_done()
queue = Queue()
opened_ports = []
for x in range(int(workers)):
threader = threading.Thread(target = thread)
threader.daemon = True
threader.start()
for worker in range(int(first_port), int(last_port)):
queue.put(worker)
queue.join()
print('\n' + 'All opened ports within the selected range:' + '\n' + '\n' +
str(opened_ports))
答案 0 :(得分:1)
只需将opened_ports = opened_ports + port
更改为opened_ports.append(port)