【发布时间】:2018-06-15 04:32:58
【问题描述】:
我有一个小代码,它会做什么,它会通过 Python 找到 nmap 端口扫描。
st = time.time()
urls_returning200 = []
urls_returning400 = []
urls_returning200.append('http://192.168.0.2')
urls_returning400.append('http://192.168.0.3')
ftpurls = []
def nmapscan(ur):
try:
nm.scan(ur, '20-25')
for host in nm.all_hosts():
print "\nHost: {0} ({1})" .format(host, nm[host].hostname())
print "Host State: %s" % nm[host].state()
for proto in nm[host].all_protocols():
port = nm[host][proto].keys()
port.sort()
print "\nPort State Service"
for ports in port:
print "{0} {1} {2}" .format(ports, nm[host][proto][ports]['state'], nm[host][proto][ports]['name'])
except KeyError as e:
print "[!] Cannot scan host!: " + ur + "\n"
print "\n...............................................................................................\n"
print "\n [PHRASE: 4]: Starts below \n\n"
nm = nmap.PortScanner()
def nm1():
global lock
lock.acquire()
print time.time()
try:
print "\n[!]Finding the Ports stats of 200's URL's \n"
for x in set(urls_returning200):
newurl = x.replace('http://', '')
nmapscan(newurl)
finally:
lock.release()
def nm2():
global lock
lock.acquire()
print time.time()
try:
print "\n...............................................................................................\n"
print "\n[!]Finding the Ports stats of 400's URL's \n"
for x in set(urls_returning400):
newurl1 = x.replace('http://', '')
nmapscan(newurl1)
finally:
lock.release
lock = threading.Lock()
if __name__ == '__main__':
th = threading.Thread(target=nm1)
th.start()
th1 = threading.Thread(target=nm2)
th1.start()
th1.join
print "overall time has been ", time.time()- st
现在我定义了 2 个函数 nm1 和 nm2 以及另一个函数,即 nmapscan,用于扫描由 nm1 和 nm2 给出的已定义 URL。我已经在两个函数的顶部定义了时间,用于获取线程开始的时间。然后我定义了两个线程,即th、th1。我希望两个线程同时启动
现在的问题是,
我不能同时发送两个线程,你可以看到两个函数的时间
1529037892.08 // time of the first function.
[!]Finding the Ports stats of 200's URL's
Host: 192.168.0.2 ()
Host State: up
Port State Service
20 closed ftp-data
21 closed ftp
22 closed ssh
23 closed telnet
24 closed priv-mail
25 closed smtp
...............................................................................................
1529037904.75 // Time of the second function,
[!]Finding the Ports stats of 400's URL's
Host: 192.168.0.4
Host State: up
overall time has been 31.6859998703
现在,我已经检查了很多次代码,但我没有发现问题。
【问题讨论】:
标签: python multithreading python-2.7 python-multithreading