def nmapspec2ports(string):
 result = set()
 for ports in string.split(','):
  if ('-' in ports):
   ports = map(int, ports.split('-', 1))
   result = result.union(xrange(ports[0], (ports[1] + 1)))
  else:
   result.add(int(ports))
 return result

def nmapspec2ports ( string ) : result = set ( ) for ports in string . split ( ',' ) : if ( '-' in ports ) : ports = map ( int , ports . split ( '-' , 1 ) ) result = result . union ( xrange ( ports [ 0 ] , ( ports [ 1 ] + 1 ) ) ) else : result . add ( int ( ports ) ) return result
this function takes a string suitable for use as argument for nmaps -p option and returns the corresponding set of ports .