线程化 Python 端口扫描器
我正在编辑使用线程的端口扫描仪时遇到问题。 这是原始代码的基础知识:
for i in range(0, 2000):
s = socket(AF_INET, SOCK_STREAM)
result = s.connect_ex((TargetIP, i))
if(result == 0) :
c = "Port %d: OPEN\n" % (i,)
s.close()
大约需要 33 分钟才能完成。所以我想我应该将它线程化以使其运行得更快一些。这是我的第一个线程项目,所以它并不算太极端,但我已经运行了以下代码大约一个小时,没有出现任何异常,也没有输出。我只是做错了线程还是什么?
import threading
from socket import *
import time
a = 0
b = 0
c = ""
d = ""
def ScanLow():
global a
global c
for i in range(0, 1000):
s = socket(AF_INET, SOCK_STREAM)
result = s.connect_ex((TargetIP, i))
if(result == 0) :
c = "Port %d: OPEN\n" % (i,)
s.close()
a += 1
def ScanHigh():
global b
global d
for i in range(1001, 2000):
s = socket(AF_INET, SOCK_STREAM)
result = s.connect_ex((TargetIP, i))
if(result == 0) :
d = "Port %d: OPEN\n" % (i,)
s.close()
b += 1
Target = raw_input("Enter Host To Scan:")
TargetIP = gethostbyname(Target)
print "Start Scan On Host ", TargetIP
Start = time.time()
threading.Thread(target = ScanLow).start()
threading.Thread(target = ScanHigh).start()
e = a + b
while e < 2000:
f = raw_input()
End = time.time() - Start
print c
print d
print End
g = raw_input()
I am having issues with a port scanner I'm editing to use threads.
This is the basics for the original code:
for i in range(0, 2000):
s = socket(AF_INET, SOCK_STREAM)
result = s.connect_ex((TargetIP, i))
if(result == 0) :
c = "Port %d: OPEN\n" % (i,)
s.close()
This takes approx 33 minutes to complete. So I thought I'd thread it to make it run a little faster. This is my first threading project so it's nothing too extreme, but I've ran the following code for about an hour and get no exceptions yet no output. Am I just doing the threading wrong or what?
import threading
from socket import *
import time
a = 0
b = 0
c = ""
d = ""
def ScanLow():
global a
global c
for i in range(0, 1000):
s = socket(AF_INET, SOCK_STREAM)
result = s.connect_ex((TargetIP, i))
if(result == 0) :
c = "Port %d: OPEN\n" % (i,)
s.close()
a += 1
def ScanHigh():
global b
global d
for i in range(1001, 2000):
s = socket(AF_INET, SOCK_STREAM)
result = s.connect_ex((TargetIP, i))
if(result == 0) :
d = "Port %d: OPEN\n" % (i,)
s.close()
b += 1
Target = raw_input("Enter Host To Scan:")
TargetIP = gethostbyname(Target)
print "Start Scan On Host ", TargetIP
Start = time.time()
threading.Thread(target = ScanLow).start()
threading.Thread(target = ScanHigh).start()
e = a + b
while e < 2000:
f = raw_input()
End = time.time() - Start
print c
print d
print End
g = raw_input()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是您的代码失败的地方。
启动线程后,立即将该值设置为
e
。但是,此后您再也不会更新e
,因此循环永远不会退出。您这样做似乎也是为了等待两个线程都完成。
join()
方法是执行此操作的更好方法。编辑:
与您的问题无关,但有帮助的评论。您的两个扫描功能都在做完全相同的事情。您可以将它们替换为一个将扫描范围作为参数的函数,并使用该函数启动两个线程。
现在您可以轻松调整要扫描的线程和端口的数量。
This is where your code is failing.
Immediately after you start your threads, you set the value to
e
. However, you never updatee
after that, so the loop never exits.It also seems like you are doing this to wait until both threads have finished. The
join()
method is is a better way to do this.Edit:
Not related to your question, but a helpful comment. Both of your scan functions are doing the exact same thing. You can replace them with one function that takes the scan range as arguments and start both threads with the one function.
And now you can easily adjust the number of threads and ports to scan.
您有一种笨拙的方法来监视线程。使用
join
将指示线程何时完成。没有理由不分出更多线程来更快地获得结果:输出
You have an awkward method for monitoring the threads. Using
join
will indicate when the thread is complete. No reason not to spin off more threads to get the results faster as well:Output