Advertisement

python发送以太网报文_Python原始套接字到以太网接口(Windows)

阅读量:

我正在尝试创建一个DHCP服务器,第一步是通过以太网端口发送数据包。我试图将数据包发送到我的以太网接口,但突然出现了一个错误。

代码如下。import socket

def sendeth(src, dst, eth_type, payload, interface = "eth0"):

"""Send raw Ethernet packet on interface."""

assert(len(src) == len(dst) == 6) # 48-bit ethernet addresses

assert(len(eth_type) == 2) # 16-bit ethernet type

#s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW)

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

From the docs: "For raw packet

sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])"

s.bind((interface, 0))

return s.send(src + dst + eth_type + payload)

if name == "main":

print("Sent %d-byte Ethernet packet on eth0" %

sendeth("\xFE\xED\xFA\xCE\xBE\xEF",

"\xFE\xED\xFA\xCE\xBE\xEF",

"\x7A\x05",

"hello"))

我对创建套接字的方式有问题。AF_包无法识别,所以我假设它只适用于Linux。我把它注释掉,在下面加了一行。我再次运行它,我开始得到一个错误如下所示。Traceback (most recent call last):

File "eth.py", line 27, in

"hello"))

File "eth.py", line 19, in sendeth

s.bind((interface, 0))

File "C:\Python27\lib\socket.py", line 224, in meth

return getattr(self._sock,name)(*args)

socket.gaierror: [Errno 11001] getaddrinfo failed

有人知道为什么会这样吗?

全部评论 (0)

还没有任何评论哟~