[Tutor] packet parsing

python@jayed.com python@jayed.com
Thu, 28 Feb 2002 15:16:13 -0600


Remco Gerlich(scarblac@pino.selwerd.nl)@2002.02.28 21:57:19 +0000:
> On  0, python@jayed.com wrote:
> > 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.
> 
> The string *is* the binary stream. A string is just a bunch of bytes.
That's what I thought.  But "print kexrecv" returns nothing in the
interpreter.  I just get ">>>" back.  "print kexrecv.capitalize()"
returns nothing.  "kexrecv.capitalize()" returns a repr() type value
"'\x00\x00..."

> kexrecv is a string of length 632.
> 
> `kexrecv` is its text representation, in a way that it could be a python
> literal - that means that say a zero byte is one character in the string
> itself, but '\x00' in the repr - three bytes longer. Try playing around with
> the string (and small parts of it) a bit more in the interpreter to get the
> idea.
OK, that makes sense.  But how do I peel of individual bytes?  (I just
played with lstrip and am not having any luck -- but this is probably my
lack of knowledge).

So `kexrecv` is the text value.  While kexrecv types as a string, I
can't seem to manipulate it as a string.  It keeps returning nothing but
">>>".

> I don't have more time atm to reply carefully to your other comments, this
> is just a quick post.
Thanks for your time.