portscan with python

import socket
host = "localhost"
ports = [21,22,23,24,25,110,80,443] # Add Port
for port in ports:
  s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  p = s.connect_ex((host,port))
  if p == 0:
    print(f"Port: {port} Open!")
  else:
    print(f"Port: {port} Filter!")

Here is what the above code is Doing:
1. We’ve created a list of ports that we want to scan.
2. We’ve created a socket object and assigned it to the variable s.
3. We’ve used the connect_ex() method to connect to the host on the specified port.
4. If the connection is successful, the connect_ex() method returns 0.
5. If the connection is unsuccessful, the connect_ex() method returns 1.
6. We’ve used an if statement to print a message if the connection is successful.
7. We’ve used an else statement to print a message if the connection is unsuccessful.