Python3 Portscanner can't solve the socket pr0blem
Question: When I run this code I am getting this socket error:

> [WinError 10038] An operation was attempted on something that is not a
> socket

but even if I delete the `s.close()` it gives me wrong results.

It is a port scanner that are going to try connecting to all ports on the
server I want to scan. And the ones that i'm getting connection from is stored
in a list. But for some reason it is giving me wrong results. can someone
please help me.

    
    
    import socket
    import threading
    
    def scan_for_open_ports():
        #Creating variables
        OpenPorts = []
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        host = input('Host to scan: ')
        global port
        global OpenPorts
        port = 1
        #Scanning
        for i in range(65534):
    
            try:
    
                s.connect((host, port))
                s.shutdown(2)
                OpenPorts.append(port)
                print(str(port) + 'is open.')
                s.close()
                port += 1
            except socket.error as msg:
                print(msg)
                s.close()
        show_user()
    def show_user():
        #Giving the user results
        print('------Open porst-----\n')
        print(OpenPorts)
    


Answer: That's because you're closing your socket inside the loop with `s.close()` and
you're not opening it again and you try to connect with a socket that's closed
already. you should close the socket when you're done with it at the end of
the loop, i also amended your code to make `OpenPorts` global and remove the
unnecessary `port` variable you define and increment inside your for loop

    
    
    import socket
    
    OpenPorts = []
    
    
    def scan_for_open_ports():
    
        # Creating variables
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        host = input('Host to scan: ')
    
        # Scanning
        for port in range(1, 65534):
            try:
                s.connect((host, port))
                OpenPorts.append(port)
                print(str(port) + 'is open.')
            except socket.error as msg:
                print(msg)
        s.close()
    
        show_user()
    
    
    def show_user():
        # Giving the user results
        print('------Open ports-----\n')
        print(OpenPorts)
    
    scan_for_open_ports()
    

