def get_pid_by_connection(src_addr, src_p, dst_addr, dst_p, proto='tcp'):
# We always take the first element as we assume it contains only one
# It should not be possible to keep two connections which are the same.
for conn in psutil.net_connections(kind=proto):
if proto == 'tcp':
if conn.laddr != (src_addr, int(src_p)):
continue
if conn.raddr != (dst_addr, int(dst_p)):
continue
# UDP gives us a very limited dataset to work with
elif proto == 'udp':
if conn.laddr[1] != int(src_p):
continue
return conn.pid
logging.warning("Could not find process for %s connection %s:%s -> %s:%s",
proto,
src_addr,
src_p,
dst_addr,
dst_p)
return None
评论列表
文章目录