lsof is a command line utility that lists open files including sockets.

List ip 4 sockets with -i 4 or ip 6 with -i 6

$ lsof -i 4

COMMAND    PID   USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
loginwind  162   user    7u  IPv4 0x1175493073f9a6ab      0t0  UDP *:*
rapportd   313   user    4u  IPv4 0x117549307415389b      0t0  TCP *:56813 (LISTEN)
SystemUIS  326   user    5u  IPv4 0x1175493077ed3df3      0t0  UDP *:61054
com.apple 6614   user   92u  IPv4 0x11754930844376cb      0t0  TCP 10.0.0.12:56330->ec2-34-193-239-11.compute-1.amazonaws.com:https (ESTABLISHED)
Python    7929   user    4u  IPv4 0x117549308e75a50b      0t0  TCP *:http-alt (LISTEN)


$ lsof -i 6

Find open sockets on ports 8000-9000

$ lsof -i :8000-9000 -P

COMMAND    PID   USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
Python    7929   user    4u  IPv4 0x117549308e75a50b      0t0  TCP *:8080 (LISTEN)


$ lsof -i :80,443 -P

COMMAND  PID   USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
commerce 380   user   11u  IPv4 0x117549308443bb43      0t0  TCP 10.0.0.12:57634->a23-194-116-90.deploy.static.akamaitechnologies.com:443 (ESTABLISHED)
commerce 380   user   16u  IPv4 0x117549308473633b      0t0  TCP 10.0.0.12:57635->a23-12-144-79.deploy.static.akamaitechnologies.com:443 (ESTABLISHED)

Find processes that opened file

$ lsof /tmp/bar.txt

COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF        NODE NAME
app     8659   user    3r   REG    1,4        0 12896073447 /private/tmp/bar.txt