MAC OS X Yosemite上的Python SOCK_RAW

时间:2015-03-07 13:37:28

标签: python python-2.7 python-3.x jpcap

我试图在Mac上用Python编写一个snipper,但是Python的SOCK_RAW在MAC OS X Yosemite上不起作用。我该怎么办?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用以下Python库:rawsocketpy。它可在Linux Python2和Python3上运行,但未在MAC OSx上进行验证。

#!/usr/bin/env python
from rawsocketpy import RawSocket

sock = RawSocket("wlp2s0", 0xEEFA)
sock.send("some data")
sock.send("personal data", dest="\xAA\xBB\xCC\xDD\xEE\xFF")

或服务器形式:

#!/usr/bin/env python
from rawsocketpy import RawRequestHandler, RawAsyncServerCallback
import time

def callback(handler, server):
    print("Testing")
    handler.setup()
    handler.handle()
    handler.finish()

class LongTaskTest(RawRequestHandler):
    def handle(self):
        time.sleep(1)
        print(self.packet)

    def finish(self):
        print("End")

    def setup(self):
        print("Begin") 

def main():
    rs = RawAsyncServerCallback("wlp2s0", 0xEEFA, LongTaskTest, callback)
    rs.spin()

if __name__ == '__main__':
    main()

脚本需要超级用户权限。