Last active
July 9, 2020 08:52
-
-
Save ramank775/93fa30b8b714f8b79d9d2ffacf037530 to your computer and use it in GitHub Desktop.
Script to get the process listening to a port number
This file contains 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_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