Skip to content

Instantly share code, notes, and snippets.

@ramank775
Last active July 9, 2020 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramank775/93fa30b8b714f8b79d9d2ffacf037530 to your computer and use it in GitHub Desktop.
Save ramank775/93fa30b8b714f8b79d9d2ffacf037530 to your computer and use it in GitHub Desktop.
Script to get the process listening to a port number
import psutil
def get_process_by_port(port):
for p in psutil.process_iter():
for c in p.connections():
if c.status == 'LISTEN' and c.laddr.port == port:
return p
return None
process_8080 = get_process_by_port(8080)
print("Pid", process_8080.pid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment