Best Python code snippet using avocado_python
factories.py
Source:factories.py  
...22        self.setOption("htype",random.choice(([1],[6])))    # set ethernet type to 10mbit (1) or IEEE 802 (6)23        self.setOption("hlen",[6])                          # Hardware Address Length: Specifies how long hardware addresses are in this message. For Ethernet or other networks using IEEE 802 MAC addresses, the value is 6.24        self.set_xid()25        if "hwaddr" in kwargs:26            self.set_hwaddr(kwargs["hwaddr"])27        if "relay_ip" in kwargs:28            self.set_hops(1)29            self.set_relay_ip(kwargs["relay_ip"])30            if "relay_vlan" in kwargs:31                self.set_relay_vlan(kwargs["relay_vlan"])32        #print self._packet_data33    def set_relay_vlan(self,vlan):34        from lib.libpydhcpserver.type_rfc import intToList35        data = [1,4,0,0]36        data = data + intToList(vlan)37        self.setOption("relay_agent",data)38    def set_relay_ip(self,ip):39        from lib.libpydhcpserver.type_rfc import ipToList40        data = ipToList(ip)41        self.setOption("giaddr",data)42    def set_xid(self,xid=0):43        import random44        from lib.libpydhcpserver.type_rfc import longToList45        if not xid:46            xid = int(random.getrandbits(32))47        data = longToList(xid)48        self.setOption("xid",data)49    def set_hwaddr(self,hwaddr):50        from lib.libpydhcpserver.type_hwmac import hwmac51        data = hwmac(hwaddr).list()52        self.setOption("chaddr",data)53    def set_hops(self,hops):54        self.setOption("hops",[hops])55class DHCPDiscoverPacketFactory(DHCPPacketFactory):56    def __init__(self,*args,**kwargs):57        super(DHCPDiscoverPacketFactory,self).__init__(*args,**kwargs)58        """59        self._packet_data = [1, 6, 6, 1, 171, 156, 47, 170, 0, 0, 128, 0, 0, 0, 0, 0, 176, 98, 80, 2, 0, 0, 0, 0, 176,60                             98, 95, 1, 0, 17, 37, 44, 24, 230, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,61                             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,62                             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,63                             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...sunos.py
Source:sunos.py  
...63                data[intf] = [addr]64            else:65                data[intf] += [addr]66        return data67    def set_hwaddr(self, i):68        if i is None or i.hwaddr != '' or ':' not in i.name:69            return i70        base_ifname, index = i.name.split(':')71        base_intf = self.interface(base_ifname)72        if base_intf is not None and len(base_intf.hwaddr) > 0:73            i.hwaddr = base_intf.hwaddr74        else:75            i.hwaddr = self.mac_from_arp(i.ipaddr)76        return i77    def mac_from_arp(self, ipaddr):78        cmd = ['/usr/sbin/arp', ipaddr]79        p = Popen(cmd, stdout=PIPE, stderr=PIPE)80        out, err = p.communicate()81        if p.returncode != 0:82            return ''83        for word in out.split():84            if ':' not in word:85                continue86            return word87        return ''88    def parse(self, out):89        i = None90        for l in out.split("\n"):91            if l == '' : break92            if l[0]!='\t' :93                i = self.set_hwaddr(i)94                (ifname,ifstatus)=l.split(': ')95                i = Interface(ifname)96                self.intf.append(i)97                # defaults98                i.link_encap = ''99                i.scope = ''100                i.bcast = ''101                i.mask = ''102                i.mtu = ''103                i.ipaddr = ''104                i.ip6addr = []105                i.ip6mask = []106                i.hwaddr = ''107                i.groupname = ''108                i.flag_up = False109                i.flag_broadcast = False110                i.flag_running = False111                i.flag_multicast = False112                i.flag_ipv4 = False113                i.flag_ipv6 = False114                i.flag_loopback = False115                if 'UP' in ifstatus : i.flag_up = True116                if 'DEPRECATED' in ifstatus : i.flag_deprecated = True117                if 'BROADCAST' in ifstatus : i.flag_broadcast = True118                if 'RUNNING' in ifstatus   : i.flag_running = True119                if 'MULTICAST' in ifstatus : i.flag_multicast = True120                if 'IPv4' in ifstatus      : i.flag_ipv4 = True121                if 'IPv6' in ifstatus      : i.flag_ipv6 = True122            else:123                n=0124                w=l.split()125                while n < len(w) :126                    [p,v]=w[n:n+2]127                    if p == 'inet' : i.ipaddr=v128                    elif p == 'netmask' : i.mask=v129                    elif p == 'broadcast' : i.bcast=v130                    elif p == 'ether' : i.hwaddr=v131                    elif p == 'groupname' : i.groupname=v132                    elif p == 'inet6' :133                        (a, m) = v.split('/')134                        i.ip6addr += [a]135                        i.ip6mask += [m]136                    n+=2137        i = self.set_hwaddr(i)138if __name__ == "__main__":139    ifaces = Ifconfig(mcast=True)...osf1.py
Source:osf1.py  
...16    def __init__(self, mcast=False):17        super(Ifconfig, self).__init__(mcast=mcast)18        out = Popen(['ifconfig', '-a'], stdin=None, stdout=PIPE, stderr=PIPE, close_fds=True).communicate()[0]19        self.parse(out)20    def set_hwaddr(self, i):21        if i is None or i.hwaddr != '':22            return i23        if ":" in i.name:24            name = i.name.split(":")[0]25        else:26            name = i.name27        cmd = ["hwmgr", "get", "attribute", "-category", "network",28               "-a", "name="+name, "-a", "MAC_address"]29        p = Popen(cmd, stdin=None, stdout=PIPE, stderr=PIPE, close_fds=True)30        out, err = p.communicate()31        if p.returncode != 0:32            return i33        for line in out.split('\n'):34            if not line.strip().startswith("MAC"):35                continue36            l = line.split("=")37            i.hwaddr = l[1].replace('-', ':').lower()38        return i39    def parse(self, out):40        i = None41        for l in out.split("\n"):42            if l == '' : continue43            if l[0]!=' ' :44                i = self.set_hwaddr(i)45                (ifname,ifstatus)=l.split(': ')46                i = Interface(ifname)47                self.intf.append(i)48                # defaults49                i.link_encap = ''50                i.scope = ''51                i.bcast = []52                i.mtu = []53                i.mask = []54                i.ipaddr = []55                i.ip6addr = []56                i.ip6mask = []57                i.hwaddr = ''58                i.flag_up = False59                i.flag_broadcast = False60                i.flag_running = False61                i.flag_multicast = False62                i.flag_ipv4 = False63                i.flag_ipv6 = False64                i.flag_loopback = False65                if 'UP' in ifstatus : i.flag_up = True66                elif 'BROADCAST' in ifstatus : i.flag_broadcast = True67                elif 'RUNNING' in ifstatus   : i.flag_running = True68                elif 'MULTICAST' in ifstatus : i.flag_multicast = True69                elif 'IPv4' in ifstatus      : i.flag_ipv4 = True70                elif 'IPv6' in ifstatus      : i.flag_ipv6 = True71            else:72                n=073                w=l.split()74                while n < len(w) :75                    [p,v]=w[n:n+2]76                    if p == 'inet' :77                        i.ipaddr.append(v)78                        i.mask.append(ipv4_bitmask(w[n+3]))79                    elif p == 'ipmtu' : i.mtu.append(v)80                    elif p == 'inet6' :81                        (a, m) = v.split('/')82                        i.ip6addr += [a]83                        i.ip6mask += [m]84                    n+=285        i = self.set_hwaddr(i)86if __name__ == "__main__":87    for c in (Ifconfig,) :...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
