#!/usr/bin/python

import socket
import sys

def checkPortsSocket(ip, portlist):
    try:
        for port in portlist:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.settimeout(5)
            result = sock.connect_ex((ip, port))
            if result == 0:
                print("Port nr {} otwarty".format(port))
            else:
                print("Port nr {} zamknięty".format(port))
            sock.close()
    except socket.error as error:
        print(str(error))
        print("Błąd połączenia")
        sys.exit()

checkPortsSocket('localhost', [21, 22, 80, 8080, 443])