python raw socket:不支持协议 [英] python raw socket: Protocol not supported
问题描述
我正在尝试在Linux下使用Python打开原始套接字.
I am trying to open a raw socket with Python under linux.
我的简单代码:
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
s.bind((HOST, 5454))
我得到了这个错误:
[ERROR] Protocol not supported
顺便说一句,我在linux 12.04下使用的是python 2.7.3,我用root来运行代码.
By the way, I am using python 2.7.3 under linux 12.04, and I used root to run the code.
有人知道吗?
更新:解决方案由
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)
也可以.
在这种情况下,您将收到带有IP和TCP标头的整个TCP数据包. 如果您使用 dstromberg 的解决方案,您还将看到以太网标头.因此,这取决于您希望数据包的原始"程度.
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.
推荐答案
尝试使用socket.AF_PACKET而不是socket.AF_INET.
Try socket.AF_PACKET instead of socket.AF_INET.
这篇关于python raw socket:不支持协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!