Created
June 20, 2024 12:16
-
-
Save chuongmep/f43b8ae0381c43025c636a6e5b784bc7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import psutil | |
def get_all_ports(): | |
ports = set() | |
for proc in psutil.process_iter(['pid', 'name']): | |
try: | |
connections = proc.net_connections() | |
for conn in connections: | |
if conn.laddr.port: | |
ports.add(conn.laddr.port) | |
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): | |
continue | |
return ports | |
if __name__ == "__main__": | |
ports_in_use = get_all_ports() | |
print("Ports in use:") | |
for port in sorted(ports_in_use): | |
print(port) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
kill ports :