简体   繁体   中英

Python3 sniffer problem in mac, why it doesn't work?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM