I am new to socket programming and I wrote this very short python3 code on mac and run. It can only sniff ping messages. When I open my Chrome and visit www.google.com it won't receive anything.
It's my code problem or a problem of macOS? And please help me with a correct version of python3 code.
import socket
sniffer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
sniffer.bind(('', 0))
sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
while True:
print(sniffer.recvfrom(4096))
I've also tried replacing IPPROTO_ICMP
with IPPROTO_IP
and IPPROTO_TCP
. None works.