Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中将以太网数据与ARP协议一起发送到汽车网关_Python_Sockets_Websocket_Compiler Errors - Fatal编程技术网

在Python中将以太网数据与ARP协议一起发送到汽车网关

在Python中将以太网数据与ARP协议一起发送到汽车网关,python,sockets,websocket,compiler-errors,Python,Sockets,Websocket,Compiler Errors,我已经用python编程,其中将发送一个字节数组来激活汽车网关中的协议功能。基本上,我正在构建一个较低级别的ISO-OSI结构,它将以太网层的数据包与ARP协议结构一起发送。我已经通过以太网LAN电缆将Raspberry pi 2连接到网关。我正在使用Raspberry中的API来运行代码。我正在使用Python3进行编译 #start of program from socket import * def sendeth(ethernet_packet,payload,interface

我已经用python编程,其中将发送一个字节数组来激活汽车网关中的协议功能。基本上,我正在构建一个较低级别的ISO-OSI结构,它将以太网层的数据包与ARP协议结构一起发送。我已经通过以太网LAN电缆将Raspberry pi 2连接到网关。我正在使用Raspberry中的API来运行代码。我正在使用Python3进行编译

#start of program

from socket import *

 def sendeth(ethernet_packet,payload,interface = "eth0"):
    """Sending RAW Ethernet packets with ARP protocol."""

    s= socket(AF_PACKET, SOCK_RAW)

    s.bind((interface,0))
    return s.send(ethernet_packet + payload)

def pack(byte_sequence):
    """convert list of bytes to byte string"""
    return b"".join(map(chr, byte_sequence))


if __name__ == "__main__":
#desadd,srcadd,ethtype    
       ethernet_packet= [0x00, 0x36, 0xf8, 0x00, 0x5b, 0xed, 0xb8, 0x27,   0xcb, 0x8c, 0x1c, 0xf9, 0x08, 0x06]  

#arpprotocol
       arp_packet = [0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01, 0xf0, 0x1f, 0xaf, 0x57, 0x33, 0xb1, 0xa9, 0xfe, 0x00, 0x14, 0x00, 0x36, 0xf8, 0x00, 0x5b, 0xed, 0xa9, 0xfe, 0x3f, 0x29] 

   payload = "".join(map(chr, arp_packet))

r = sendeth(pack(ethernet_packet),
            pack(arp_packet))
printf("sent Etherent with ARP_Paket payload length is %d bytes" % r)
当我用我的raspberry PI运行程序时

$sudo python3 APR.py    
它抛出了一个错误

Traceback (most recent call test):  
File"APR.py", line 24,in <module>  
r = sendeth(pack(ethernet_packet),  
File"APR.py", line 13,in pack  
return b"".join(map(chr, byte_sequence))  
TypeError: sequence intem 0: expected Bytes, bytearray, or an object with   the buffer Interface, str found.
回溯(最近的呼叫测试):
文件“APR.py”,第24行,在
r=发送(数据包(以太网_数据包),
文件“APR.py”,第13行,打包
返回b“”。连接(映射(chr,字节_序列))
TypeError:sequence intem 0:应为字节、bytearray或具有缓冲区接口的对象,找到str。
我已经在Google和Wikipedia上为这个看似合理的错误尽了最大的努力,但没有找到任何错误。我在Python上才一个月大。因此,在这个问题上的任何线索或帮助都将非常有用和方便

可能解

因为我使用的是python 3,所以它抛出了一个错误。在python 2中,它的perfectlf很好。可能在python 3中,我无法将十六进制作为字符串发送!!如果这是一个问题,有人能帮我克服这个错误吗。

你的pack()函数不能工作,因为
chr
函数不能工作 返回
str
,但不是字节字符串。有关可能的解决方案,请参阅

在您的情况下,最简单的解决方案是直接使用
bytearray

r = sendeth(bytearray(ethernet_packet),
        bytearrayarp_packet))

我已经试过使用bytearray了,但是出现了相同的错误。@qwerty我重写了答案。