packing hex value for socket.send

V

Volkan YAZICI

hi all,

i've been working on a communication server to talk with an io controller
device. i've written a quick and dirty php script a while ago and now
i need to port it to python (for a real comm. server program). but i
couldn't achieve to send commands over opened socket connection by converting
them to raw format using pack command.

the working php function looks like:

// {{{ php code
$commandList = array('01', '01', '00', '00', '00', '08', '3D', 'CC');
foreach ( $commandList as $hexnum ) {
/*
The mean of 'H*' differs from the one in pythons pack command.
I pasted below explanation from php documentation:
i. The repeater argument can be either an integer value or * for
repeating to the end of the input data.
ii. Code: H
Description: Hex string, high nibble first.
*/
$buf = pack('H*', $hexnum);
socket_write($socket, $buf, strlen($buf));
}
// }}}

I've tried below commands in python, but they didn't work:

# {{{ python code
"""
i know command list differs from the below one but
that doesn't matter. because there's already dosens
of these ones.
"""
cmdList1 = ("01", "05", "00", "01", "FF", "FF", "9D", "BA")
cmdList2 = (0x01, 0x05, 0x00, 0x01, 0xFF, 0xFF, 0x9D, 0xBA)

for command in cmdList1:
code = pack("cc", command)
channel.send(code)

for command in cmdList2:
"""
i'm converting hex command to integer, because
pack command doesn't recognize hex values.
"""
command = int(command)
code = pack("I", command)
channel.send(code)
# }}}

There doesn't exist a `H' code and `*' param. in pythons pack function (please
correct if i'm wrong), so i stucked in here. I'd be so appreciated if somebody
could help to fix that problem.

best regards

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFA8j6NGcWq5WdHoacRAiZAAJ931lw3psZIRlYjaa5VNWAVJDBLpACgoPCl
EbhXbOTcU8Z1jVgW8MSSTd4=
=o7Rm
-----END PGP SIGNATURE-----
 
P

Paul Rubin

Volkan YAZICI said:
There doesn't exist a `H' code and `*' param. in pythons pack
function (please correct if i'm wrong), so i stucked in here. I'd be
so appreciated if somebody could help to fix that problem.

See the docs for the binascii module. You want the binascii.unhexlify
function, I think.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,619
Members
45,312
Latest member
Svsdvsdvs

Latest Threads

Top