Re: help on packet format for tcp/ip programming

2007-02-08 Thread rattan
On Feb 8, 3:40 am, Grant Edwards [EMAIL PROTECTED] wrote:
 On 2007-02-08, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:

  struct module pack and unpack will only work for fixed size buffer :
  pack('1024sIL', buffer, count. offset) but the buffer size can vary
  from one packet to the next  :-(

 Oh for Pete's sake...

 struct.pack('%dsIL' % len(buffer), buffer, count, offset)

 --
 Grant Edwards   grante Yow!  I want the presidency
   at   so bad I can already taste
visi.comthe hors d'oeuvres.

that is great but how does one unpack on the other side?

-ishwar

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help on packet format for tcp/ip programming

2007-02-08 Thread Diez B. Roggisch
[EMAIL PROTECTED] wrote:

 On Feb 8, 3:40 am, Grant Edwards [EMAIL PROTECTED] wrote:
 On 2007-02-08, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:

  struct module pack and unpack will only work for fixed size buffer :
  pack('1024sIL', buffer, count. offset) but the buffer size can vary
  from one packet to the next  :-(

 Oh for Pete's sake...

 struct.pack('%dsIL' % len(buffer), buffer, count, offset)

 --
 Grant Edwards   grante Yow!  I want the
 presidency
   at   so bad I can already
   taste
visi.comthe hors d'oeuvres.
 
 that is great but how does one unpack on the other side?

By peeking into the header, determining the number of bytes necessary?

But why do you need this anyway - if you know that you will have python on
both ends, use Pyro or xmlrpc.

Diez
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help on packet format for tcp/ip programming

2007-02-08 Thread Jean-Paul Calderone
On Thu, 08 Feb 2007 15:56:30 +0100, Diez B. Roggisch [EMAIL PROTECTED] 
wrote:
[EMAIL PROTECTED] wrote:

 On Feb 8, 3:40 am, Grant Edwards [EMAIL PROTECTED] wrote:
 On 2007-02-08, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:

  struct module pack and unpack will only work for fixed size buffer :
  pack('1024sIL', buffer, count. offset) but the buffer size can vary
  from one packet to the next  :-(

 Oh for Pete's sake...

 struct.pack('%dsIL' % len(buffer), buffer, count, offset)

 --
 Grant Edwards   grante Yow!  I want the
 presidency
   at   so bad I can already
   taste
visi.comthe hors d'oeuvres.

 that is great but how does one unpack on the other side?

By peeking into the header, determining the number of bytes necessary?

But why do you need this anyway - if you know that you will have python on
both ends, use Pyro or xmlrpc.


Grant had the right idea, I think, but he failed to actually include a
byte length in his format. :)  So there's nothing to peek at.  If the
packing is done like this, instead..


struct.pack('!IIL', len(buffer), count, offset) + buffer

Then it is a simple matter to unpack it once the receiving side, by waiting
for struct.calcsize('!IIL') bytes, using struct to get len(buffer), count,
and offset:

length, count, offset = struct.unpack('!IIL', bytes)

And then waiting for `length' more bytes, which will be the buffer.

I'm not sure what the original use-case was here.  XML-RPC isn't a good
transport for arbitrary binary data.  If `buffer' contains text, though,
that might be a good suggestion.

Jean-Paul
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help on packet format for tcp/ip programming

2007-02-08 Thread Diez B. Roggisch
 Grant had the right idea, I think, but he failed to actually include a
 byte length in his format. :)  So there's nothing to peek at.  If the
 packing is done like this, instead..
 
 
 struct.pack('!IIL', len(buffer), count, offset) + buffer
 
 Then it is a simple matter to unpack it once the receiving side, by
 waiting for struct.calcsize('!IIL') bytes, using struct to get
 len(buffer), count, and offset:
 
 length, count, offset = struct.unpack('!IIL', bytes)
 
 And then waiting for `length' more bytes, which will be the buffer.

That was my intention, yes - I thought the header information of the OP
contained a byte count already.
 
 I'm not sure what the original use-case was here.  XML-RPC isn't a good
 transport for arbitrary binary data.  If `buffer' contains text, though,
 that might be a good suggestion.

Certainly XMLRPC isn't too good - and Pyro in many aspects better. AFAIK it
uses pickle, and that means that things should be comparably compact.

Diez
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help on packet format for tcp/ip programming

2007-02-08 Thread Grant Edwards
On 2007-02-08, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
 On Feb 8, 3:40 am, Grant Edwards [EMAIL PROTECTED] wrote:
 On 2007-02-08, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:

  struct module pack and unpack will only work for fixed size buffer :
  pack('1024sIL', buffer, count. offset) but the buffer size can vary
  from one packet to the next  :-(

 Oh for Pete's sake...

 struct.pack('%dsIL' % len(buffer), buffer, count, offset)

 that is great but how does one unpack on the other side?

struct.unpack('%dsIL' % buflen ,packet)

-- 
Grant Edwards   grante Yow!  Yow! Did something
  at   bad happen or am I in a
   visi.comdrive-in movie??
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help on packet format for tcp/ip programming

2007-02-08 Thread Grant Edwards
On 2007-02-08, Diez B. Roggisch [EMAIL PROTECTED] wrote:

 struct module pack and unpack will only work for fixed size buffer :
 pack('1024sIL', buffer, count. offset) but the buffer size can vary
 from one packet to the next  :-(

 Oh for Pete's sake...

 struct.pack('%dsIL' % len(buffer), buffer, count, offset)

 
 that is great but how does one unpack on the other side?

 By peeking into the header, determining the number of bytes necessary?

Better yet, use a protocol that's not brain-dead: send the
buffer size _before_ you send the buffer.

 But why do you need this anyway - if you know that you will
 have python on both ends, use Pyro or xmlrpc.

-- 
Grant Edwards   grante Yow!  A dwarf is passing
  at   out somewhere in Detroit!
   visi.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help on packet format for tcp/ip programming

2007-02-07 Thread Bjoern Schliessmann
[EMAIL PROTECTED] wrote:

 I want a specific packet format for packet exchange between a
 client server across the network. For example frame format
 as a python class could be:
 class Frame:
 def __init__(self, buffer=None, count=0, offset=0):
 self.buffer = buffer
 self.count = count
 self.offset = offset
 the question is how to convert it to a byte stream so that format
 of count and offset also becomes a sequence of bytes.

Try struct.

Regards,


Björn

-- 
BOFH excuse #208:

Your mail is being routed through Germany ... and they're censoring
us.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help on packet format for tcp/ip programming

2007-02-07 Thread rattan
On Feb 8, 1:43 am, Bjoern Schliessmann usenet-
[EMAIL PROTECTED] wrote:
 [EMAIL PROTECTED] wrote:
  I want a specific packet format for packet exchange between a
  client server across the network. For example frame format
  as a python class could be:
  class Frame:
  def __init__(self, buffer=None, count=0, offset=0):
  self.buffer = buffer
  self.count = count
  self.offset = offset
  the question is how to convert it to a byte stream so that format
  of count and offset also becomes a sequence of bytes.

 Try struct.

 Regards,

 Björn

 --
 BOFH excuse #208:

 Your mail is being routed through Germany ... and they're censoring
 us.

struct module pack and unpack will only work for fixed size buffer :
pack('1024sIL', buffer, count. offset) but the buffer size can vary
from one packet to the next  :-(

-ishwar

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help on packet format for tcp/ip programming

2007-02-07 Thread Felipe Almeida Lessa
On 7 Feb 2007 19:14:13 -0800, [EMAIL PROTECTED] [EMAIL PROTECTED]
 struct module pack and unpack will only work for fixed size buffer :
 pack('1024sIL', buffer, count. offset) but the buffer size can vary
 from one packet to the next  :-(

Then send the size of the buffer before the buffer, so the recipient
can expect that many bytes.

-- 
Felipe.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help on packet format for tcp/ip programming

2007-02-07 Thread Gabriel Genellina
En Thu, 08 Feb 2007 00:14:13 -0300, [EMAIL PROTECTED] escribió:

 On Feb 8, 1:43 am, Bjoern Schliessmann usenet-
 [EMAIL PROTECTED] wrote:
 [EMAIL PROTECTED] wrote:
  I want a specific packet format for packet exchange between a
  client server across the network. For example frame format
  as a python class could be:
  class Frame:
  def __init__(self, buffer=None, count=0, offset=0):
  self.buffer = buffer
  self.count = count
  self.offset = offset
  the question is how to convert it to a byte stream so that format
  of count and offset also becomes a sequence of bytes.

 Try struct.


 struct module pack and unpack will only work for fixed size buffer :
 pack('1024sIL', buffer, count. offset) but the buffer size can vary
 from one packet to the next  :-(

Use struct to pack count and offset into a fixed size field (2 or 4 bytes  
depending on range); send buffer after that.
buffer must come *after* you send its size, else it's a lot harder to  
retrieve at the other end.

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help on packet format for tcp/ip programming

2007-02-07 Thread Grant Edwards
On 2007-02-08, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:

 struct module pack and unpack will only work for fixed size buffer :
 pack('1024sIL', buffer, count. offset) but the buffer size can vary
 from one packet to the next  :-(

Oh for Pete's sake...

struct.pack('%dsIL' % len(buffer), buffer, count, offset)

-- 
Grant Edwards   grante Yow!  I want the presidency
  at   so bad I can already taste
   visi.comthe hors d'oeuvres.
-- 
http://mail.python.org/mailman/listinfo/python-list