import socket
import sys

host = (sys.argv[1])
portRng = (sys.argv[2])

strtPrt, endPrt = portRng.split("-")

def port_scan(hostname, portRange):

    portDict = {}
    strtPrt, endPrt = portRng.split("-")
    s = socket.socket()

    for Nport in range(int(strtPrt), int(endPrt)):
        try:
            s.connect((hostname, Nport ))
            data = s.recv(1024)
            portDict[Nport] = data.decode('utf-8')
            s.close
        except:
            pass

    if len(portDict) != 0:
        print ('Open ports are:')
        print ('PORT', 'SERVICE', sep='\t')
        for k, v in portDict.items():
            print (k, v, sep='\t')
    else:
        print ('No open ports at range.')

port_scan(host, portRng)
