0

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.

maxhsia
  • 23
  • 3

1 Answers1

0

I met a similar problem like yours when I was programming in C on MacOS

Strange RAW Socket on Mac OS X says that almost all the packets, registered in kernel(including TCP), won't pass rock sockets. So, you can have a try to pcap in Python.

Frank OU
  • 41
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 29 '22 at 19:02
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/31890560) – Emi OB Jun 01 '22 at 08:00