Error: An operation was attempted on some item that is not a socket!

0

I was trying to make a simple port scanner, but when it found a port open, the errors that appeared changed from [WinError 10061] No connection could be made because the target machine actively refused them to [WinError 10038] An operation was attempted on some item that is not a socket!

def ScanPorts(host,Range):
i=1
while i <= int(Range):
    try:
        s.connect((host , i))
        print(i)
        lista_portas.append(i)
        s.close()
        time.sleep(1)
        print(i)
    except Exception as e :
        print(str(i) + ":" + str(e))
    i=i+1
for i in lista_portas:
    print('Porta:',i,"aberta")
    
asked by anonymous 11.03.2017 / 22:05

1 answer

0

You should redo / create another socket for a different connection, I also adjusted the code a little bit based on what you put:

import socket

host = 'google.com'
rg = range(70, 100)
open_ports = []
def ScanPorts(host,rg):
    for i in rg:
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.settimeout(1)
            s.connect((host,i))
            print('Success with port', i)
        except Exception as err:
            print('port closed', i)
        else:
            open_ports.append(str(i))
    return open_ports

open_ports = ScanPorts(host, rg)
print('Portas ABertas:\n{}'.format('\n'.join(open_ports)))
    
11.03.2017 / 22:54