简体   繁体   English

在jython udp数据包中发送原始十六进制数据

[英]Send Raw hex data in jython udp packet

I'm experience with Java, but I have to integrate a java library into a co-workers python code. 我对Java有经验,但是我必须将Java库集成到同事的python代码中。 Enter jython, yay! 输入jython,对!

We are trying to send a UDP packet with a very specific data section. 我们正在尝试发送包含非常具体的数据部分的UDP数据包。

We are building up the packet like so: 我们正在像这样构建数据包:

version = 0x0001
referenceNumber = 0x2323
bookID = byteArray('df82818293819dbafde818ef')

For easy of explanation assume byteArray takes in a string of hex digits and returns a byte array 为了便于说明,假定byteArray接受一个十六进制数字的字符串并返回一个字节数组

We then build the packet: 然后,我们构建数据包:

packet = hex(version)
packet += hex(referenceNumber)
packet += bookID

and send it out the socket. 并将其发送到套接字。

I know this is incorrect, the data types can't be right ,so the concat wont do the right thing. 我知道这是不正确的,数据类型不能正确,因此concat不会做正确的事情。 How do we build up this packet properly? 我们如何正确构建此数据包? The python documentation say that s.sendTo() takes a string? python文档说s.sendTo()需要一个字符串吗? I think I want an alternative to s.sendTo() that takes a byte array. 我想我想要一个采用字节数组的s.sendTo()替代方法。

We want the packet to arrive at the server with the udp data section looking like: 我们希望数据包以udp数据段的形式到达服务器:

00 01 23 23 df 82 81 82 93 81 9d ba fd e8 18 ef

What is the proper approach to do this in python? 在python中执行此操作的正确方法是什么?

We are using wireshark to verify the packet arrives properly and right now the udp data section looks as if python converts each field as an ascii representation. 我们正在使用Wireshark来验证数据包是否正确到达,现在udp数据部分看起来就像python将每个字段转换为ascii表示一样。 For example, the referenceNumber field comes accross as the ascii values for the literal string '0x2323'. 例如,referenceNumber字段作为文字字符串'0x2323'的ascii值出现。 Which makes sense because s.sendTo() takes a string. 这很有意义,因为s.sendTo()需要一个字符串。

====================SOLUTION============================== ===================解决方案============================== =

Yup, that does it... shows how new I am to python. 是的,做到了……展示了我对python的了解。 For the curious, here is the code: 出于好奇,下面是代码:

version = '0001'
referenceNumber = '2323'

packet = a2b_hex(version)
packet += a2b_hex(referenceNumber)

.. etc

then just 然后就

 s.send(packet)

检出binascii模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM