简体   繁体   中英

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. Enter jython, yay!

We are trying to send a UDP packet with a very specific data section.

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

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. How do we build up this packet properly? The python documentation say that s.sendTo() takes a string? I think I want an alternative to s.sendTo() that takes a byte array.

We want the packet to arrive at the server with the udp data section looking like:

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?

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. For example, the referenceNumber field comes accross as the ascii values for the literal string '0x2323'. Which makes sense because s.sendTo() takes a string.

====================SOLUTION==============================

Yup, that does it... shows how new I am to 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模块。

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