2
我在线上发现了这个代码,发现它在OSX上不起作用。有没有人知道没有使用第三方库的正确方法?python:在OSX上使用原始套接字
import socket
import struct
import binascii
rawSocket = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(0x0003))
while True:
packet = rawSocket.recvfrom(2048)
ethernet_header = packet[0][0:14]
ethernet_detailed = struct.unpack(“!6s6s2s”, ethernet_header)
arp_header = packet[0][14:42]
arp_detailed = struct.unpack(“2s2s1s1s2s6s4s6s4s”, arp_header)
# skip non-ARP packets
ethertype = ethernet_detailed[2]
if ethertype != ‘\x08\x06’:
continue
source_mac = binascii.hexlify(arp_detailed[5])
dest_ip = socket.inet_ntoa(arp_detailed[8])
if source_mac == ‘74c24671971c’:
print “Tide button pressed!, IP = “ + dest_ip
apparantly OSX没有AF_PACKET或PF_PACKET和AF_INET太高级别为了这个,我想,或者至少是需要比在更换下降更加重编码。
谢谢
你可以发布一个链接到github的要点?我有同样的问题。 – seren
编辑答案与要点链接 – Neal