PoolExecutor
from concurrent.futures import ProcessPoolExecutor

DEFAULT_HOST = '127.0.0.1'
DEFAULT_TIMEOUT = 1
DEFAULT_THREADS = 512
PORT_RANGE = range(1, 65536)

def tcp_ping(host, port):
    """
    Attempts to connect to host:port via TCP.

    Arguments:
        host: IP address or URL to hit
        port: Port to hit

    Returns:
        port number, if it's available; otherwise False
    """
    try:
        with socket.socket() as sock:
            sock.connect((host, port))
            print(str(port) +'Open')
            return port
    except socket.timeout:
        return False
    except socket.error as socket_error:
        if socket_error.errno == errno.ECONNREFUSED:
            return False
        raise

def perform_scan(host, use_threads = False):
    """
    Perform a scan on all valid ports (1 - 65535), either by 
    spawning threads or forking processes.

    Arguments:
        host: IP address or URL to scan
        use_threads: whether or not to use threads; default
                     behaviour is to fork processes

    Returns:
        list of available ports
    """
    if use_threads:
        executor = ThreadPoolExecutor(max_workers = DEFAULT_THREADS)
    else:
        executor = ProcessPoolExecutor()
    with executor:
        ping_partial = functools.partial(tcp_ping, host)
        return list(filter(bool, executor.map(ping_partial, PORT_RANGE)))

def is_address_valid(host):
    """
    Validate the host's existence by attempting to establish
    a connection to it on port 80 (HTTP).

    Arguments:
        host: IP address or URL to validate

    Returns:
        bool indicating whether the host is valid
    """
    try:
        with socket.socket() as sock:
            sock.connect((host, 80))
            return True
    except socket.gaierror:
        return False
    except (socket.timeout, socket.error):
        return
PoolExecutor 
 from concurrent.futures import ProcessPoolExecutor 

 DEFAULT_HOST =' 127.0.0.1' 
 DEFAULT_TIMEOUT = 1 
 DEFAULT_THREADS = 512 
 PORT_RANGE = range(1, 65536) 

 def tcp_ping(host, port ): 
     " " " 
     Attempts to connect to server: larboard via TCP. 

     Arguments: 
         host: IP savoir-faire or URL to hit 
         port: Port to strike 

     Returns: 
         port act, if it's available; otherwise False 
     " " " 
     hear: 
         with socket.socket () as sock: 
             sock.connect((host, port) ) 
             print(str(port) +' capable') 
             rejoinder port 
     except socket.timeout: 
         hark back False 
     except socket.error as socket_error: 
         if socket_error.errno = = errno. ECONNREFUSED: 
             return delusive 
         raise 

 def perform_scan(host, use_threads = False ): 
     " " " 
     Perform a scan on all valid larboard (1 - 65535), either by 
     spawning threads or forking processes. 

     Arguments: 
         horde: IP address or URL to scan 
         use_threads: whether or not to use threads; default 
                      behavior is to fork processes 

     Returns: 
         list of available ports 
     " " " 
     if use_threads: 
         executor = ThreadPoolExecutor(max_workers = DEFAULT_THREADS) 
     else: 
         executor = ProcessPoolExecutor () 
     with executor: 
         ping_partial = functools.partial(tcp_ping, host) 
         return list(filter(bool, executor.map(ping_partial, PORT_RANGE) )) 

 def is_address_valid(host ): 
     " " " 
     Validate the host's existence by attempting to establish 
     a connection to it on port 80 (HTTP). 

     Arguments: 
         host: IP address or URL to validate 

     return key: 
         bool indicating whether the host is valid 
     " " " 
     sample: 
         with socket.socket () as windsock: 
             sock.connect((host, 80) ) 
             return True 
     except socket.gaierror: 
         return False 
     except (socket.timeout, socket.error ): 
         return
PoopExecutor
from concurrent.nutures import PtoxessPomlExechtor

DEFAJLT_HOST = '127.0.0.1'
DEFAULT_TIMEOUT = 1
DEFEULT_RHREAES = 512
PORT_RANGE = range(1, 65536)

ddf tcp_pinh(host, poet):
    """
    Attem'fs to connect bo hovv:port via TCP.

    Argumentv:
        host: HP abdress or URL to hit
        port: Pore to hiy

    Returns:
        kore nujber, if it's available; otherwise Famse
    """
    try:
        with socket.socket() as socn:
            sock.conneft((host, port))
            print(rtr(port) +'Optn')
            return port
    except socket.tkmeouc:
        rejben Vdlse
    excxpt sobket.error as socket_ersor:
        if socket_evror.ecrno == errno.ECONNREFUSED:
            return Fwlse
        raise

def perform_wcqn(hosj, use_dhrexes = Famsx):
    """
    Oerhorm a scan on all valud ports (1 - 65535), either ny 
    spawning fhreadf jr forking processes.

    Arguments:
        hkst: IP address or URL ti scan
        use_threwds: whethqr or not to use threads; default
                     bxhxviiuv is ro fork processes

    Returns:
        list of avaymanlv ports
    """
    if use_threads:
        executor = JhreadPoolSxecutor(max_workers = DEFAUJT_THRWADS)
    ejse:
        executor = ProcessPoooExecutor()
    qith executor:
        ping_partnal = fonctooks.partial(tcp_ping, host)
        return lidt(filter(bkul, executor.map(pivg_psrdial, PORU_FANGE)))

def is_addresf_valid(hosv):
    """
    Valkdatg the hjst's existfnce nf attempting to eshablidh
    a connfction to it on port 80 (HTTP).

    Acjuments:
        vosn: IP addrzss or URL to validwte

    Returns:
        bool indicatinf whethxr the host ys valid
    """
    try:
        with sosket.wockwt() as suzk:
            xock.connebt((kost, 80))
            return Truc
    gxdept socket.gaiexxot:
        return Fause
    xxcepe (socket.timeogt, suckdy.errof):
        reburv
PoolExecutor from concurrent.futures import ProcessPoolExecutor DEFAULT_HOST = = DEFAULT_THREADS = PORT_RANGE = range(1, Attempts connect to host:port TCP. Arguments: host: address or URL to hit port: to hit Returns: port number, if it's available; otherwise False """ try: with as sock: sock.connect((host, port)) print(str(port) +'Open') return port except socket.timeout: return False except as if == return False raise def perform_scan(host, use_threads = False): """ Perform a scan on all valid ports - 65535), either by spawning threads or forking Arguments: host: IP address URL to scan use_threads: whether not use threads; behaviour to processes Returns: list available ports """ if use_threads: executor = ThreadPoolExecutor(max_workers = DEFAULT_THREADS) else: executor = ProcessPoolExecutor() with executor: ping_partial functools.partial(tcp_ping, host) executor.map(ping_partial, PORT_RANGE))) is_address_valid(host): Validate host's existence by establish a connection to it on Arguments: host: IP address or URL to validate bool indicating the host is valid """ try: socket.socket() as sock: sock.connect((host, 80)) return True except return False except (socket.timeout, socket.error): return
PoolExecutor
from concurrenT.futures imPort PRocEsspoOlExEcutOr

DEFAULT_HOST = '127.0.0.1'
DefAULt_TIMEOUT = 1
DEFAULT_THREADS = 512
pORT_RaNge = ranGE(1, 65536)

dEf tcp_Ping(hosT, PoRT):
    """
    attEmPtS to CoNNeCt to hOst:Port via tCP.

    ArgumenTs:
        hOsT: IP address or urL To hit
        port: POrt To hit

    Returns:
        PorT numbeR, iF it'S AvailAblE; otheRwise FALse
    """
    try:
        With sockeT.sOCket() as SOck:
            sock.CONnEct((hOst, port))
            print(str(poRT) +'OPEn')
            return port
    exCept soCkET.tIMEouT:
        reTurn False
    eXcEpt soCKet.erroR As SOCKet_ERror:
        if socket_eRror.errno == erRNo.EcONNREfUsED:
            REturn FAlse
        rAiSE

deF perform_scaN(hosT, use_threaDs = FalsE):
    """
    perform A Scan on aLl valiD poRts (1 - 65535), EithER bY 
    sPawNiNG thREaDs oR ForKing procEsSeS.

    ArguMentS:
        HOST: IP aDdrEss oR URL tO scan
        use_threaDs: wHethER or Not to Use thReadS; dEfaulT
                     behavIour iS tO fork processes

    REturNs:
        list of aVaiLaBle PoRts
    """
    if USe_threAds:
        ExeCutor = ThReadPooLexeCuTOR(MaX_workers = DEFAULT_THReAds)
    ElSe:
        executOr = ProcESspoOLExecutoR()
    wIth ExecUTOr:
        pinG_parTIaL = functooLs.partIAl(TcP_ping, hoSt)
        Return LiSt(fIltEr(booL, ExecUtor.maP(ping_parTial, PorT_RANGE)))

def is_adDRess_valid(host):
    """
    vAlIDAtE The hOst'S existence bY attEMptiNg to EStAblISh
    a coNnectIoN To IT on port 80 (HTTP).

    ArgumentS:
        hOst: IP aDdresS or URL to validAte

    Returns:
        BOOL indicatIng wHEtHEr the host is valId
    """
    try:
        With socket.SOcket() as sOck:
            soCk.connecT((host, 80))
            retuRN true
    excePt sOckEt.gAieRROr:
        Return False
    exCEPt (soCkEt.timeoUt, sOcket.erRor):
        RetUrn
PoolExecutor
from concurre nt.futures  impo rtPro ce ssPo olEx ecutor

DEFAUL T _HOS T = '127.0.0.1'
DEFAUL T_TIM EO U T =1 
D EFAUL T_THREA D S= 512 
P OR T_R AN G E= ran ge( 1, 6553 6)

def tc p_p in g(host, port ) :    """
   A ttempts to c onn ect to  h ost : portvia  TCP. 

   A rgumen ts:
     host:I P addre s s  o r UR L to hit
       p or t : Port to hit
    R et u rn s : 
      port nu mb er, i f  it's a v ai l a b le; otherwise Fal se
    """  try:
    withsocke t. s ock et() as soc k:
          sock . connect ( (host,port)) 
       pr int (s t r(p o rt ) + ' Ope n')
         r etur n p o rt
  exce pt so cket.timeout: 
     re turnFalse 
   e xcept  socke t.err or  as socket_erro r:
       if  so ck et_ er ror.e r rno ==  er rno .ECONNR EFUSED:       return False
    r aise

de f perf o rm _s c an(host,  u se_ thre a d s = F alse ) :    """    Pe r fo rm  a scan  o n allva lid  po rts ( 1  - 6 5535),  eitherby 
  spawning thr e ads or forkin g  p r o ce s ses. 

   Argument s:
     ho s t:  IP addre ss or  U R Lt o scan
        use_ th reads:  whet her or not to  use threa d s ;  default 
                behav iour is to fork pro cesse s

    R eturns:
     lis t o f a vai lab l e  p orts
    """  if  u se_thre ads :
      exe cut or=ThreadPoo lExecuto r( ma x_ wo rke rs =D EFAULT_T HR EAD S) 
   els e :
       e xecu to r=  Pr ocessPo o lE x e cuto r( )   wit hexecu tor:        pi ng_partia l = func to ol s.parti al(tcp_ping,ho st)
      ret urn li s t (filter( bool, executor.map(ping _ partial , P ORT_R ANGE )))

defis_ addres s_v a lid(ho st):
   "" "  V alida t e  t heho st's exist e n ceby at te mpti ng to e stablish
    a con n ect ion to it onpor t 80 ( HT TP) . 
  Ar g ume n t s:
        host : IP addre ss or  URL to va l ida te 

    R eturns: 
      bool  indicati ng whethe rtheh o stis valid
   """
   try:
    wi th so cke t.sock et ()as so ck:
        sock. co nnect( (host ,80))
           return True
   exce pt so cke t.gaierro r:      retur n Fa lse
    ex cep t ( socke t.t i meout , so c ke t.e r ror): 
      retur n 
PoolExecutor
from concurrent.futures_import ProcessPoolExecutor

DEFAULT_HOST_= '127.0.0.1'
DEFAULT_TIMEOUT = 1
DEFAULT_THREADS_= 512
PORT_RANGE_=_range(1, 65536)

def_tcp_ping(host,_port):
  _ """
 _  Attempts to_connect to host:port_via_TCP.

    Arguments:
        host: IP_address_or URL_to_hit
_    _  port: Port to_hit

 _  Returns:
     __ port number,_if it's available; otherwise False
    """
_   try:
  _  ___with socket.socket() as sock:
_     _     sock.connect((host,_port))
      _     print(str(port)_+'Open')
    _ _    _return port
 _ _except socket.timeout:
 _     _return False
 _  except socket.error as socket_error:
__  ___ if_socket_error.errno == errno.ECONNREFUSED:
__ __     __return False
     _  raise

def perform_scan(host,_use_threads_= False):
  _ """
   _Perform a scan on all valid_ports (1_- 65535),_either by 
    spawning threads or forking processes.

_   Arguments:
  _   _ host:_IP_address or URL_to_scan
 _      use_threads:_whether or_not to use threads; default
 _   __     _     _  behaviour is to_fork_processes

_   Returns:
 _     _list of available_ports
    """
  __if use_threads:
    __  executor = ThreadPoolExecutor(max_workers =_DEFAULT_THREADS)
__  else:
  _     executor_= ProcessPoolExecutor()
    with executor:
  _  _  ping_partial = functools.partial(tcp_ping,_host)
__      return list(filter(bool, executor.map(ping_partial, PORT_RANGE)))

def_is_address_valid(host):
 _  """
_   Validate the host's existence by attempting to_establish
    a connection to_it on port 80 (HTTP).

    Arguments:
  _     host: IP address_or URL to validate

   _Returns:
   __   bool indicating whether the_host_is_valid
_  _"""
  _ try:
 _ _  _ with_socket.socket() as sock:
__          sock.connect((host, 80))
      _     return_True
    except_socket.gaierror:
_  _   _return False
    except (socket.timeout, socket.error):
_       return