23 lines
731 B
Python
23 lines
731 B
Python
"""
|
|
Force kill all Python processes on port 8000
|
|
"""
|
|
import psutil
|
|
import os
|
|
import signal
|
|
|
|
def kill_process_on_port(port):
|
|
"""Kill process listening on specified port"""
|
|
for conn in psutil.net_connections():
|
|
if conn.laddr.port == port and conn.status == 'LISTEN':
|
|
try:
|
|
proc = psutil.Process(conn.pid)
|
|
# Kill the process and all its children
|
|
proc.kill()
|
|
print(f"Killed process {conn.pid} on port {port}")
|
|
except (psutil.NoSuchProcess, psutil.AccessDenied) as e:
|
|
print(f"Could not kill process {conn.pid}: {e}")
|
|
|
|
# Kill all processes on port 8000
|
|
kill_process_on_port(8000)
|
|
print("Port 8000 should now be free")
|