Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

else: IndentationError: unexpected unindent

Can someone explain why else is getting IndentationError: unexpected unindent while i try to work further on my if else loop.

Basicly am working on a port scanner project where i need to get some user input where out of this user input, i will specify how many ports there is needed to scan.
My plan was to work further on the if command, so if i do not get the response of 1 the meaning was to next. But it do not seem like to work

Whats wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

input = input("1 or 2")

if input == '1':
        try:
                for port in range(1,65535):
                        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                        socket.setdefaulttimeout(1)

                        result = s.connect_ex((target,port))

                        if result ==0:

                                print("Port {} is open".format(port))

                        s.close()

else:
        print("do not work")

        except KeyboardInterrupt:
                print("\n ")
                sys.exit()
        except socket.gaierror:
                print("\n ")
                sys.exit()
        except socket.error:
                print("\")
                sys.exit()

>Solution :

I’d have to say it’s that you’ve got an else in between your except statements, separating it from the try… right?
Try something like this maybe

input = input("1 or 2")

if input == '1':
        try:
                for port in range(1,65535):
                        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                        socket.setdefaulttimeout(1)

                        result = s.connect_ex((target,port))

                        if result ==0:

                                print("Port {} is open".format(port))

                        s.close()

        except KeyboardInterrupt:
                print("\n ")
                sys.exit()
        except socket.gaierror:
                print("\n ")
                sys.exit()
        except socket.error:
                print("\n ")
                sys.exit()

else:
        print("doesn’t work")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading