Starting Python... some questions

Tim Roberts timr at
Tue Mar 13 07:59:27 CET 2007

You only need three things here:

jezonthenet at wrote:
>import scapy
>import struct
>class lldp_class:
>	def __init__(self):
>		self.chassis_id_tlv = None
>	def chassis_id(subtype, chassis_info):

Make that
        def chassis_id(self, subtype, chassis_info):

When you call "p.chassis_id", "p" is passed as the first parameter to
chassis_id.  By convention, we call that "self", in the same way the object
of a C++ method is called "this" (although that is enforced, rather than

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

Make that
  def main():

The underscores have a special meaning that you do not need here.  Note
that the name "main" is not required; some people call it something like

>	p = lldp_class()
>	p.chassis_id(4, "01:80:C2:00:00:0E")
>	payload = p.chassis_id_tlv
>	ether = scapy.Ether(dst="01:02:03:04:05:06")
>	fullpayload = ether + payload
>	sendp(fullpayload)

Now, at the end, add:

if __name__=="__main__":

That should do it.
Tim Roberts, timr at
Providenza & Boekelheide, Inc.

More information about the Python-list mailing list