Starting Python... some questions

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Mar 13 03:05:04 EDT 2007


En Tue, 13 Mar 2007 03:13:37 -0300, jezzzz . <jezonthenet at yahoo.com>  
escribió:

> I added the self parameter to the the chassis_id function, however I now  
> have a problem and question regarding encoding MAC addresses for a  
> struct. What string format am I supposed to use?

py> data = [int(x,16) for x in "2A:3B:01:9C:04:F0".split(":")]
py> print data
[42, 59, 1, 156, 4, 240]
py> struct.pack("6B", *data)
'*;\x01\x9c\x04\xf0'

That is: take the string "2A:3B:01:9C:04:F0", split it in parts at each  
":", iterate over them converting from hex to integer; the resulting list  
is called data.
Then, pack the numbers using "6B" format.

> Also, I think I will get rid of Scapy in my small program. Any  
> references I can use to easily send (no need to receive) a packet with a  
> Layer 1 and 2 only? (i.e. only Ethernet and LLDP - no need for IP, UDP,  
> TCP etc. I wish to create something like Ethernet | LLDP msg1 | LLDP  
> msg2 | ... | LLDP msgn)

I don't think you can do that with Python alone - that is, without using  
some other specific tool like scapy.

> class lldp_class:

Usually classes have UppercaseWords, and omit the word "class".
But since LLDP would be all uppercase, maybe another name would be better.
You appear to be storing just a few attributes, not implementing that  
protocol, so maybe LLDPInfo might be used.

>     def chassis_id(self, subtype, chassis_info):
>
>         if subtype == 4:
>
>             chassis_data = struct.pack("!B",chassis_info)
>
>         subtype_data = struct.pack("!B",subtype)
>
>         self.chassis_id_tlv = subtype_data + chassis_data

What should happen when subtipe is not 4? (Currently you get an error).

-- 
Gabriel Genellina




More information about the Python-list mailing list