Python scanner would like advice

Hello I am programming a simple python port scanner and I am having issues first the code

#!/usr/bin/python
import subprocess
import socket
from termcolor import colored

subprocess.call('clear', shell=True)

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

socket.setdefaulttimeout(2)

host = input(colored("[*] Enter Your Victim's Host to Scan: ", 'magenta'))
#old single port
#port = int(raw_input("[*] Enter The Victim's Port:  "))

def portscanner(port):
        if sock.connect_ex((host,port)) :
                pass
        else:
                print(colored("[*] Port %d is open" % (port), 'blue'))
for port in range(1,65536):
        portscanner(port)

When i run this on my localhost it shows the first port open

result:

[*] Enter Your Victim's Host to Scan: localhost
[*] Port 902 is open

but when I run nmap it shows this result:

Nmap scan report for localhost (127.0.0.1)
Host is up (0.00016s latency).
Other addresses for localhost (not scanned): ::1
Not shown: 65528 closed tcp ports (conn-refused)
PORT      STATE SERVICE
902/tcp   open  iss-realsecure
903/tcp   open  iss-console-mgr
40815/tcp open  unknown

so my question:

Why is my scanner only detecting the FIRST open port and specifying that all the other ports are closed?

Thank you again for help! :slight_smile: