NameError全局名称PortScanner未定义,但已定义

时间:2017-07-13 22:46:17

标签: python-2.7

我的以下代码存在此问题。

Traceback (most recent call last):
  File "RecursoveScan.py", line 28, in <module>
    class PortScanner(threading.Thread):
  File "RecursoveScan.py", line 95, in PortScanner
    main()
  File "RecursoveScan.py", line 83, in main
    thread = PortScanner()
NameError: global name 'PortScanner' is not definedTraceback (most recent call last):
  File "RecursoveScan.py", line 28, in <module>
    class PortScanner(threading.Thread):
  File "RecursoveScan.py", line 95, in PortScanner
    main()
  File "RecursoveScan.py", line 83, in main
    thread = PortScanner()
NameError: global name 'PortScanner' is not defined

但是在第26行,我有这个定义。请帮忙。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import time
import argparse
import socket
import threading
import thread

parser = argparse.ArgumentParser(description='Single Host Service scan')      
parser,add_argument('-host',action='store',dest='host',required=True,help='Set Remote Host IP')
parser.add_argument('-portrange',action='store',dest='portrange',required=True,help='Set Portscan Range')
parser.add_argument('-threads',action='store',dest='thread',type=int,required=True,help='Set Number of Threads (10-100)')
args = parser.parse_args()

class PortScanner(threading.Thread):
    openportcount = 0


    def __init__(self, hostname, portrange):
        threading.Thread.__init__(self)
        self.hostname = args.host
        self.portrange = args.portrange

    def run(self):
        while True:
            for port in range(self.portrange[0],self.portrange[1]):
                sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                print status
                if status == 0:
                    print "Port %d      |OPEN|"%port
                    PortScanner.openportcount+=1
                    sock.close()
                else:
                    print "Port %d      |CLOSED|"%port
                    PortScanner.closedportcount+=1
                    socket.close()


    def main():
        print("[*] Port Scanner Starting..\n")
        portrange = list((args.portrange.split("-")))


        lport = int(portrange[0])
        uport = int(portrange[1])

        if lport>uport:
            tempport = uport
            uport = lport
            lport = tempport

        if uport > 65535:
            uport = 65535
            print"[!] Port ranges must be 0-65535\n[!] Port Range Set To %d - 65535\n"%lport
        elif lport < 0:
            lport = 0
        print"[!] Port ranges must be 0-65535\n[!] Port range set to 0 - %d\n"%uport

        no_of_threads = args.thread

        r = (uport -lport)/no_of_threads
        print"\n[+] %d Threads Starting.\n"%no_of_threads

        threads = []
        for i in range(10,no_of_threads):
            uport = lport +r+ 1
            if uport>65535:
                uport=65535
            ports = [lport, uport]
            thread = PortScanner()
            lport = uport+1
            thread.start()
            threads.append(thread)

        for t in threads:
            t.join()

        print("\n %d Open Ports Found!"%PortScanner.openportcount)
        print("\n %d Closed Ports Detected!"%PortScanner.closedportcount)
        print("\nDONE..")

    main()

我一遍又一遍地试过,但仍然无法弄清楚为什么它会给我这个错误。我对python很新,而不是最好的。感谢是否有人可以提供帮助。

0 个答案:

没有答案