[Python] Use python to parse someip messages and print the messages in someip format

1. Install the scapy library

Use pip to install scapy third-party library, open cmd and enter the following command:

pip install scapy

If the image shown in the figure appears, the installation is successful:

Insert image description here

2. Parse someip format messages

To parse someip format messages, you need to import the someip module:

from scapy.contrib.automotive.someip import *
# 导入someip模块
from scapy.contrib.automotive.someip import * 

# 普通格式的someip报文
data = b"\x07\xff\x80\x01\x00\x00\x00S\x00\x00\x00\x06\x01\x01\x02\x00\x0c\xf1\xdds\x84\x00\x00\x00>[\x19\xa2\xd1aV\xce'\xc1)\xa9x02Eg\x00\x00\x00D"

someipData  = SOMEIP(_pkt=data) # 解析普通格式报文为someip格式
someipData.show() # 打印someip格式的报文

The printed someip format message is as follows:

Insert image description here

3.Example

The following messages are sent and received through. The example is as follows:udpsomeip

1.New construction textudp_server.py,User model callserverend

from scapy.contrib.automotive.someip import * 
import socket

udpServer = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 创建socket对象,走udp通道
host = socket.gethostname() # 获取本地主机名
port = 1000
serverAddr = (host, port)
udpServer.bind(serverAddr) # 绑定服务端地址

clientData, clientAddr = udpServer.recvfrom(1024) # 接收来自客户端的数据
print(clientData) # 打印普通格式报文
someipData = SOMEIP(_pkt=clientData)
someipData.show() # 打印someip格式报文
udpServer.sendto(clientData, clientAddr) # 发送数据给客户端
udpServer.close()
    

2.New construction textudp_client.py,User model callclientend

import socket

udpClient = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 创建socket对象,走udp通道
host = socket.gethostname() # 获取本地主机名
port = 1000
serverAddr = (host, port)

# someip报文
data = b"\x07\xff\x80\x01\x00\x00\x00S\x00\x00\x00\x06\x01\x01\x02\x00\x0c\xf1\xdds\x84\x00\x00\x00>[\x19\xa2\xd1aV\xce'\xc1)\xa9x02Eg\x00\x00\x00D"

udpClient.sendto(data, serverAddr) # 发送报文给服务端
udpClient.close()
    

3. Open two cmd windows, one is the server side window and the other is the client side window< /span>

Running sequence, start the server first, then the client:

  • In the server side window, execute the command firstpython udp_server.py
  • Client window, then execute the commandpython udp_client.py

The running results are as follows:

Server side window:

Insert image description here

Client window:

Insert image description here

Guess you like

Origin blog.csdn.net/aidijava/article/details/132308041