我试图在Python中编写一个非常简单的端口扫描程序,直到我决定引入线程才能加快速度。
我想在最后添加一个简短的摘要,其中列出了打开的端口。我把它放在一个名为finish的函数中。但是,自引入线程以来,无论我把它放在哪里,该摘要都显示为我的代码输出中的第一行。
有没有办法可以有效地将线程限制在我需要它的函数中,然后在它到达summary / finish()之前关闭它,或者我只是犯了一个明显的错误?任何帮助将非常感激。
代码:
from socket import *
from threading import *
screenLock = Semaphore(value=1)
open_ports = []
def scan(ip,port):
try:
s = socket(AF_INET, SOCK_STREAM)
s.connect((ip, port))
screenLock.acquire()
print ('Scanning ', ip , 'on port', port)
print("Port",port, "is open")
s.close()
summary(port)
except:
screenLock.acquire()
print ('Scanning ', ip , 'on port', port)
print("Port",port,"is closed")
finally:
screenLock.release()
s.close()
def loop():
for i in range(1,100):
ip = '192.168.0.38'
port = int(i)
t = Thread(target=scan, args=(ip,int(port)))
t.start()
return
def summary(port):
global open_ports
open_ports.append(port)
return
def main():
loop()
finish()
def finish():
print('The following ports are open:',open_ports)
main()
答案 0 :(得分:1)
您必须等待所有线程完成:
select i.Id, i.Prayaseeid, i.name, i.Gender, c.Category,
i.ideadescription, i.Domain, i.ProjectTerms, i.ProjectStartDate, i.Amountsanctioned,
id.Amount from tb_innovatorDisbursement
i.projectstatus, i.projectoutcome
from tb_innovator i left join
tb_Category c
on i.category = c.id left join
tb_innovatorDisbursement id
on id.tbid = i.dbid and id.applyingforcycle = '1'
where i.tbid = 'TBINO1111A 'and i.applyingforcycle = '1';