python编写的一个通过多线程扫描端口的代码
发布时间:2020-05-24 23:56:30 所属栏目:Python 来源:互联网
导读:python编写的一个通过多线程扫描端口的代码
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 #!/usr/bin/env python
import socket
import sys
import threading
import time
NORMAL = 0
ERROR = 1
TIMEOUT = 5
def ping(ip,port,timeout=TIMEOUT):
try:
cs=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
cs.settimeout(float(timeout))
address=(str(ip),int(port))
status = cs.connect_ex((address))
if status == NORMAL :
print "%d is NORMAL" %port
except Exception,e:
print ERROR
print "error:%s" %e
return ERROR
cs.close()
return NORMAL
class Scan(threading.Thread):
def __init__(self,ip,timeout):
threading.Thread.__init__(self)
self.ip = ip
self.timeout = timeout
def run(self):
global p_begin,p_end,mutex
threadname = threading.currentThread().getName()
while 1:
mutex.acquire()
p_begin += 1
if p_begin > p_end:
mutex.release()
break
mutex.release()
ping(self.ip,p_begin,self.timeout)
if __name__ == '__main__':
if len(sys.argv) < 4:
print 'format:urlbegin portbegin portend timeout'
urlbegin = str(sys.argv[1])
portbegin = int(sys.argv[2])
portend = int(sys.argv[3])
timeout = float(sys.argv[4])
global p_begin,mutex
threads = []
num = 10
p_begin = portbegin
p_end = portend
mutex = threading.Lock()
for x in xrange(0,num):
t_scan = Scan(urlbegin,timeout)
t_scan.setDaemon(True)
threads.append(t_scan)
for t in threads:
t.start()
for t in threads:
t.join()
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
