Mathyou
Mathyou

Reputation: 653

Python Localhost refuses connection

My code for a port scanner is as follows:

    import socket as sk

    for port in range(1, 1024):
        try:
            s = sk.socket(sk.AF_INET,sk.SOCK_STREAM)
            s.settimeout(1000)
            s.connect(("127.0.0.1",port))
            print("{0}:OPEN".format(port))
            s.close
        except: continue

I get this error:

    s.connect(("127.0.0.1",port))
    ConnectionRefusedError: [WinError 10061] No connection could be made 
    because the target machine actively refused it   

Why is this??

Upvotes: 0

Views: 222

Answers (1)

Mathyou
Mathyou

Reputation: 653

Klaus D. pointed me in the right direction. I had to create a try/except statement around the line creating the error, allowing me to continue. This happened because a closed port created an error, ending the program. The exception allowed me to continue checking ports after each error.

Upvotes: 1

Related Questions