Error: An operation has been attempted on an item that is not a socket!

Asked

Viewed 1,139 times

1

I was trying to make a very simple door scanner, [Winerror 10061] No connection could be made because the destination machine actively refused them (q was the expected) for [Winerror 10038] An operation has been attempted on an 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")
  • 1

    You are using python 3?

1 answer

1

You must redo/create another socket for a different connection, I also set the code a 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)))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.