简体   繁体   中英

python raw socket: Protocol not supported

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.

尝试使用socket.AF_PACKET而不是socket.AF_INET。

This runs without error as root:

#!/usr/local/cpython-3.3/bin/python

import socket as socket_mod

#s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
socket = socket_mod.socket(socket_mod.AF_PACKET, socket_mod.SOCK_RAW, socket_mod.IPPROTO_IP)
#socket.bind(('localhost', 5454))
socket.bind(('lo', 5454))

试试socket.AF_UNIX,它可以解决您的问题,祝您好运。

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