当前位置:编程学习 > python >>

PortScanner in Python 3.1

摘自:んㄗ葑訫鎖愛S Blog

 import socket
import sys
import threading

MAX_THREADS = 50

def usage():
    print( Port Scanner 1.0)
    print(usage:PyScan.py <host> [start port] [end port])

class Scanner(threading.Thread):
    def __init__(self,host,port):
        threading.Thread.__init__(self)
        self.host = host
        self.port = port
        self.sd = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

    def run(self):
        try:
            self.sd.connect((self.host,self.port))
            print(%s:%d open % (self.host,self.port))
            self.sd.close()
        except:pass
class PyScan:
    def __init__(self,args=[]):
        self.args = args
        self.start,self.stop = 1,1024
        self.host = ""

        if len(self.args) == 4:
            self.host = self.args[1]
            try:
                self.start = int(self.args[2])
                self.stop = int(self.args[3])
            except ValueError:
                usage()
                return
            if self.start > self.stop:
                uasge()
                return
        elif len(self.args) == 2:
            self.host = self.args[1]
        else:
            usage()
            return

        try:
            socket.gethostbyname(self.host)
        except:
            print("Hostname %s unknow" % self.host)
        self.scan(self.host,self.start,self.stop)
    def scan(self,host,start,stop):
        self.port = start
        while self.port <= stop:
            try:   #死了都要try
                while threading.active_count() < MAX_THREADS:
                    try:
                        Scanner(host,self.port).start()
                        self.port += 1
                    except(KeyboardInterrupt,SystemExit):
                        socket.socket.close()
                        sys.exit(1)
                       
            except:
                sys.exit(1)
                   
           
                   
if __name__ == "__main__":
    PyScan(sys.argv)

\

补充:Web开发 , Python ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,