Search code examples
pythonpython-3.xfunctionmoduleattributeerror

AttributeError: module 'prtscnr' has no attribute 'portscanner'


It's part of my login.py module. I imported my prtscnr module at the top of the code.

import prtscnr

def menu():
    print("****** Recon ******")
    print("1- PortScanner")
    print("2- Subdomain Finder")
    print("3- Hash Decryption ")
    print("4- Çıkış ")
    msecim = int(input("Seçimini yap: "))
    if msecim == 1 :
        print("Portscanner'a hoşgeldin...")
        prtscnr.portscanner()

It's my prtscnr.py module. I defined portscanner function in it. But when i tried to run it in my login.py it gives me AttributeError: module 'prtscnr' has no attribute 'portscanner'. What should I do?


#upgrade. dosya adını ve konumunu kullanıcıdan al. loopu hızlandır.
import socket

if __name__ == "__main__":
    def portscanner():
    # port aralığını başlangıç ve bitiş değerlerine ayır
        target = input("Hedefin IP adresini veya URL'ini girin: ")
        port_range = input("Taranacak olan port aralığını girin (örnek: 1-1000): ")
        start_port, end_port = map(int, port_range.split("-"))

    # açık portları tutmak için bir liste oluştur
        open_ports = []

    # aralıktaki tüm portlar için for döngüsü
        for port in range(start_port, end_port+1):
        # socket oluştur
            client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        # bağlantı girişimi için timeout
            client.settimeout(0.5)
        # hedef IP/Hostname'e bağlantı girişimi
            try:
                response = client.connect_ex((target, port))
            # Bağlantı başarılıysa 0 döndürür ve açık olan portları terminalde yazar
                if response == 0:
                    open_ports.append(port)
                    print("Port {0}: Açık".format(port))
            # socketi kapat
                client.close()
            except:
                pass

    # Açık port numaralarını .txt dosyası içine yazar
        dosya_adi = input("Dosya adını girin: ")
        dosya_konumu = input("Dosya konumunu girin: ")
        with open(dosya_konumu + "\\" + dosya_adi + ".txt", "w") as file:
            file.write("{0} için port {1} den port {2} e kadar olan açık portlar:\n".format(target, start_port, end_port))
            for port in open_ports:
                file.write("{0}\n".format(port))
            print("Open port numbers have been written to {0}".format(dosya_adi))
       

when i try to run prtscnr.portscanner() code in to my login.py file i got an error like AttributeError: module 'prtscnr' has no attribute 'portscanner' what's wrong? or what should i do?


Solution

  • Your problem is that you check if __name__ == '__main__' before defining your function in prtscnr.py.

    What checking if __name__ == '__main__' is used for is to check whether the file being called has been imported by an external file (in which case __name__ will be equal to the name of the file) or if it is being called directly as the first file of the stack trace (in this case __name__ is equal to __main__).

    Here is a demonstration:

    main.py

    import file1
    print(f'from main file: {__name__}')
    

    file1.py

    print(f'from file1: {__name__}')
    

    When I run main.py the output is:

    from file1: file1
    from main file: __main__  
    

    So that is why your portscanner method is not recognized. If you intend to call the file only ever as an import you can completely get rid of the if __name__ == '__main__' or replace it with if __name__ == 'prtscnr'.

    I hope this helps!