(port, process_name=None)
Terminate a process that is bound to a port.
    
    The process name can be set (eg. python), which will
    ignore any other process that doesn't start with it.
Terminate a process that is bound to a port.
    
    The process name can be set (eg. python), which will
    ignore any other process that doesn't start with it.
Terminate a process that is bound to a port . The process name can be set ( eg . python ) which will ignore any other process that doesn t start with it .
def force_close_port(port, process_name=None):
    """Terminate a process that is bound to a port.
    
    The process name can be set (eg. python), which will
    ignore any other process that doesn't start with it.
    """
    for proc in psutil.process_iter():
        for conn in proc.connections():
            if conn.laddr[1] == port:
                #Don't close if it belongs to SYSTEM
                #On windows using .username() results in AccessDenied
                #TODO: Needs testing on other operating systems
                try:
                    proc.username()
                except psutil.AccessDenied:
                    pass
                else:
                    if process_name is None or proc.name().startswith(process_name):
                        try:
                            proc.kill()
                        except (psutil.NoSuchProcess, psutil.AccessDenied):
                            pass
def force_close_port ( port , process_name = None ) : for proc in psutil . process_iter ( ) : for conn in proc . connections ( ) : if conn . laddr [ 1 ] == port : #Don't close if it belongs to SYSTEM #On windows using .username() results in AccessDenied #TODO: Needs testing on other operating systems try : proc . username ( ) except psutil . AccessDenied : pass else : if process_name is None or proc . name ( ) . startswith ( process_name ) : try : proc . kill ( ) except ( psutil . NoSuchProcess , psutil . AccessDenied ) : pass