python 多线程端口扫描 2种

代码堆里睡觉的人 发布于 2013/01/02 18:50
阅读 1K+
收藏 12

不多说直接上代码吧

 

#!/usr/local/bin/python
#-*- coding: UTF-8 -*-
#QQ316118740     这个速度较快
import sys
import socket
import thread
import time
socket.setdefaulttimeout(3)  #设置了全局默认超时时间
def socket_port(ip,PORT):  #扫描开放端口
    #try:
        if PORT>=65535:
            print u"端口扫描结束"
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        #s.settimeout(float(1))  #延时5S
        #s.connect((ip,PORT))
        result=s.connect_ex((ip,PORT))
        if(result==0):
            print ip,u":",PORT,u"端口开放"
        s.close()
    #        xxx = open('ip.txt','a+')  #open file
#        xxx.seek(1)
#        xxx.write(str(ip))
#        xxx.write('\n')
#        xxx.close()
    #except:
    #    info=sys.exc_info()
        #print "",
        #sys.stdout.write("Hello World")
        #print ip,u":",PORT,u"端口未开放",
        #self.textEdit_4.append(ip)

if __name__=='__main__':
    IP="127.0.0.1"
    t=time.time()
    for i in range(0, 65535 + 1):
        thread.start_new_thread(socket_port,(IP,int(i)))
        time.sleep(0.003) #确保先运行Seeker中的方法

    print 'used time:%f' % (time.time()-t)

###################################################################3

#!/usr/local/bin/python
#-*- coding: UTF-8 -*-
#QQ316118740
import time
from socket import *
from threading import Thread
from Queue import Queue
def scan(ip,port):
    s=socket(AF_INET,SOCK_STREAM)
    result=s.connect_ex((ip,port))
    if(result==0):
        print 'Port %d: OPEN' % port
    s.close()
class Worker(Thread):
    def __init__(self,taskQueue):
        Thread.__init__(self)
        self.setDaemon(True)
        self.taskQueue=taskQueue
        self.start()

    def run(self):
        while True:
            try:
                callable,args,kwds=self.taskQueue.get(block=False)
                callable(*args,**kwds)
            except:
                break
class ThreadPool:
    def __init__(self,ip):
        self.threads=[]
        self.taskQueue=Queue()
        self.threadNum=3
        self.__create_taskqueue(ip)
        self.__create_threadpool(self.threadNum)

    def __create_taskqueue(self,ip):
        for i in range(0,65535):
            self.add_task(scan,ip,i)

    def __create_threadpool(self,threadNum):
        for i in range(threadNum):
            thread=Worker(self.taskQueue)
            self.threads.append(thread)

    def add_task(self,callable,*args,**kwds):
        self.taskQueue.put((callable,args,kwds))

    def waitfor_complete(self):
        while len(self.threads):
            thread=self.threads.pop()
            thread.join()
            if thread.isAlive() and not self.taskQueue.empty():
                self.threads.append(thread)
        print 'scaning is over!'

if __name__=='__main__':
    #target=raw_input('Enter host to scan:')  #抱歉,系统响应超时,请稍后再试
    #targetIP=gethostbyname(target)
    t=time.time()
    targetIP="127.0.0.1"
    print 'Starting scan on host',targetIP
    tp=ThreadPool(targetIP)
    tp.waitfor_complete()
    print 'used time:%f' % (time.time()-t)

 

加载中
0
代码堆里睡觉的人
代码堆里睡觉的人

#!/usr/local/bin/python
#-*- coding: UTF-8 -*-
#QQ316118740     这个速度较快
import sys
import socket
import thread
import time
socket.setdefaulttimeout(3)  #设置了全局默认超时时间
def socket_port(ip,PORT):  #扫描开放端口
    #try:
        if PORT>=65535:
            print u"端口扫描结束"
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        #s.settimeout(float(1))  #延时5S
        #s.connect((ip,PORT))
        result=s.connect_ex((ip,PORT))
        if(result==0):
            print ip,u":",PORT,u"端口开放"
        s.close()
    #        xxx = open('ip.txt','a+')  #open file
#        xxx.seek(1)
#        xxx.write(str(ip))
#        xxx.write('\n')
#        xxx.close()
    #except:
    #    info=sys.exc_info()
        #print "",
        #sys.stdout.write("Hello World")
        #print ip,u":",PORT,u"端口未开放",
        #self.textEdit_4.append(ip)

if __name__=='__main__':
    IP="127.0.0.1"
    t=time.time()
    for i in range(0, 65535 + 1):
        thread.start_new_thread(socket_port,(IP,int(i)))
        time.sleep(0.003) #确保先运行Seeker中的方法

    print 'used time:%f' % (time.time()-t)

0
代码堆里睡觉的人
代码堆里睡觉的人

#!/usr/local/bin/python
#-*- coding: UTF-8 -*-
#QQ316118740
import time
from socket import *
from threading import Thread
from Queue import Queue
def scan(ip,port):
    s=socket(AF_INET,SOCK_STREAM)
    result=s.connect_ex((ip,port))
    if(result==0):
        print 'Port %d: OPEN' % port
    s.close()
class Worker(Thread):
    def __init__(self,taskQueue):
        Thread.__init__(self)
        self.setDaemon(True)
        self.taskQueue=taskQueue
        self.start()

    def run(self):
        while True:
            try:
                callable,args,kwds=self.taskQueue.get(block=False)
                callable(*args,**kwds)
            except:
                break
class ThreadPool:
    def __init__(self,ip):
        self.threads=[]
        self.taskQueue=Queue()
        self.threadNum=3
        self.__create_taskqueue(ip)
        self.__create_threadpool(self.threadNum)

    def __create_taskqueue(self,ip):
        for i in range(0,65535):
            self.add_task(scan,ip,i)

    def __create_threadpool(self,threadNum):
        for i in range(threadNum):
            thread=Worker(self.taskQueue)
            self.threads.append(thread)

    def add_task(self,callable,*args,**kwds):
        self.taskQueue.put((callable,args,kwds))

    def waitfor_complete(self):
        while len(self.threads):
            thread=self.threads.pop()
            thread.join()
            if thread.isAlive() and not self.taskQueue.empty():
                self.threads.append(thread)
        print 'scaning is over!'

if __name__=='__main__':
    #target=raw_input('Enter host to scan:')  #抱歉,系统响应超时,请稍后再试
    #targetIP=gethostbyname(target)
    t=time.time()
    targetIP="127.0.0.1"
    print 'Starting scan on host',targetIP
    tp=ThreadPool(targetIP)
    tp.waitfor_complete()
    print 'used time:%f' % (time.time()-t)

0
Jooooooker
Jooooooker
都是tcp哦
返回顶部
顶部