packing hex value for socket.send

Volkan YAZICI yazicivo at itu.edu.tr
Mon Jul 12 03:32:29 EDT 2004


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20040712/43f8cb5d/attachment.sig>


More information about the Python-list mailing list