[Tutor] packet parsing

python@jayed.com python@jayed.com
Thu, 28 Feb 2002 14:53:26 -0600


Hello,

I'm trying to make the best of my unemployed time and have decided that
implementing an SSH client in Python will keep me occupied for a few
years.

I've just started, and am working on the first non-trivial part (which
appeared REALLY quickly in the IETF draft).

My program connects to the server; the server says, "I'm an SSH server".
My client says, "I'm an SSH client".  The server then sends a variable
length message in SSH Binary Packet Format to initiate a key exchange.

My first question is:  do I need to be concerned about
little/big/network-endian?  Both of my machines are little-endian.  So
when I look at a byte from the SSH server's packet, do I need to do a
network-to-host translation?  Conversely, when I send a byte to the
server, do I need to do a host-to-network translation

My second question is: how do I look at/manipulate bytes of an incoming
network packet?  Or bits even.  Right now, I have the following piece of 
code that receives the SSH server's initial key exchange packet:

    kexrecv = mysock.recv(blocksize) 

I want to directly manipulate the bits/bytes of kexrecv.  Everything
that I've done makes kexrecv into a string.  And I'm having problems
with kexrecv as a string -- I want to deal with it as a binary stream.
(Note:  this might not be the best way to deal with it, but until I have
a good grip on the SSH transport protocol, it's how I prefer to deal
with it -- right now I want to be able to say: "read the first 32 bits,
convert it into a number; read the next 8 bits, convert it into a
number; read the next 128 bits...blah, blah, etc.).

type(kexrecv) results in <type 'str'>
type(`kexrecv`) results in <type 'str'>
len(kexrecv) returns nothing
len(`kexrecv`) returns 830
print len(kexrecv) returns 632
print len(`kexrecv`) returns 830

(Yes, I know that the backticks are equivalent to repr() -- I looked it
up on google last night).  I want to deal with kexrecv and not
`kexrecv`.  But I haven't been able to figure it out.

If kexrecv is a string [as type(kexrecv) seems to think] why doesn't
len(kexrecv) return anything?  And if kexrecv is a string, why does
len(`kexrecv`) return a larger value than "print len(kexrecv)"?

Thanks,
Jay