[Baypiggies] A python newbie questoin

Ashok Chippa a.n.chippa at gmail.com
Sat Feb 21 01:09:49 CET 2015


Thank you Alex, Jeff and Nam. Just after sending the question, I suspected
it may be the type instead of length... Thank you for your answers, I think
I have enough to fix it now... :-)

Thanks a lot,

On Fri, Feb 20, 2015 at 3:54 PM, Alex Martelli <aleax at google.com> wrote:

> struct.pack returns *a string* and struct.unpack accepts *a string*.  buf
> is *a list*, not a string.  struct.unpack[buf[-1]] (or buf[0] as buf only
> has one item -- the string in question).  No idea of how it can work in the
> case you say it works:-).
>
> Alex
>
> On Fri, Feb 20, 2015 at 3:49 PM, Ashok Chippa <a.n.chippa at gmail.com>
> wrote:
>
>> A newbie python question:
>>
>> THIS WORKS:
>>
>> ethernet.py:
>>
>> ...
>> def test():
>>     buf = []
>>     #eth_gen(buf)
>>     buf = struct.pack('!6s6sH', '\x01\x02\x03\x04\x05\x06', \
>>                                 '\x07\x08\x09\x0a\x0b\x0c', 0x0800)
>>     eth = Eth()
>>     eth.unpack(buf)
>>     eth.show()
>>
>> root at ashok-vb:/home/achippa/ppl# python ethernet.py
>> smac = 07:08:09:0a:0b:0c dmac = 01:02:03:04:05:06 type = 0800
>>
>>
>> THIS DOES NOT WORK:
>>
>> # Build an ethernet header into the specified buffer.
>> #
>> def eth_gen(buf):
>>     n = len(buf)
>>     buf[n:] = struct.pack('!6s6sH', '\x01\x02\x03\x04\x05\x06', \
>>                                     '\x07\x08\x09\x0a\x0b\x0c', 0x0800)
>> def test():
>>     buf = []
>>     eth_gen(buf)
>>     eth = Eth()
>>     eth.unpack(buf)
>>     eth.show()
>>     #
>>
>> root at ashok-vb:/home/achippa/ppl# !py
>> python ethernet.py
>> Traceback (most recent call last):
>>   File "ethernet.py", line 181, in <module>
>>     test()
>>   File "ethernet.py", line 173, in test
>>     eth.unpack(buf)
>>   File "ethernet.py", line 89, in unpack
>>     dpkt.Packet.unpack(self, buf)
>>   File "/usr/local/lib/python2.7/dist-packages/dpkt/dpkt.py", line 127,
>> in unpack
>>     struct.unpack(self.__hdr_fmt__, buf[:self.__hdr_len__])):
>> struct.error: unpack requires a string argument of length 14
>>
>> I have verified (by printing) that calcsize(self.__hdr_fmt__) and
>> self.__hdr_len__ are both 14. The buf has only 14 bytes of ethernet header.
>> Putting the struct.pack(..) in a function (eth_gen()) causes the error...
>>
>> Any idea what may be happening... Thanks a lot
>>
>> _______________________________________________
>> Baypiggies mailing list
>> Baypiggies at python.org
>> To change your subscription options or unsubscribe:
>> https://mail.python.org/mailman/listinfo/baypiggies
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20150220/4278a76b/attachment.html>


More information about the Baypiggies mailing list