python raw socket_python raw socket:不支持协议

I am trying to open a raw socket with Python under linux.

My simple code:

s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)

s.bind((HOST, 5454))

And I got this error:

[ERROR] Protocol not supported

By the way, I am using python 2.7.3 under linux 12.04, and I used root to run the code.

Does anyone have a clue?

Update: The solution given by dstromberg is correct. If you want the whole packet, then use his solution. However, there is another combination:

s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)

that also works.

In this case, you will receive a whole TCP packet with IP and TCP headers on it.

If your use dstromberg's solution, you will also see the ethernet header. So it depends on how 'raw' you want your packet to be.

解决方案

Try socket.AF_PACKET instead of socket.AF_INET.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值