Best Python code snippet using autotest_python
net_tc.py
Source:net_tc.py  
...115    def get_handle(self):116        return self._handle117    def set_handle(self, handle):118        self._handle = handle119    def _get_tc_conf(self, netif):120        if self._tc_conf:121            return self._tc_conf122        self._tc_conf = dict()123        self._tc_conf[tcfilter.conf_device] = netif.get_name()124        self._tc_conf[tcfilter.conf_parent] = self._parent_qdisc.id()125        self._tc_conf[tcfilter.conf_type] = self.filtertype126        self._tc_conf[tcfilter.conf_protocol] = self._protocol127        self._tc_conf[tcfilter.conf_priotity] = self._priority128        self._tc_conf[tcfilter.conf_flowid] = (129            self._dest_qdisc.get_parent_class().id())130        return self._tc_conf131    def tc_cmd(self, tc_conf):132        print self._tc_cmd % tc_conf133    def setup(self, netif):134        pass135    def restore(self, netif):136        pass137class u32filter(tcfilter):138    filtertype = 'u32'139    def __init__(self):140        super(u32filter, self).__init__()141        self._rules = []142    def _filter_rules(self):143        return ' \\\n  '.join(self._rules)144    def add_rule(self, rule):145        self._rules.append(rule)146    def setup(self, netif):147        tc_conf = self._get_tc_conf(netif)148        tc_conf[tcfilter.conf_cmd] = 'add'149        tc_conf[tcfilter.conf_rules] = self._filter_rules()150        self.tc_cmd(tc_conf)151    def restore(self, netif):152        tc_conf = self._get_tc_conf(netif)153        tc_conf[tcfilter.conf_cmd] = 'del'154        tc_conf[tcfilter.conf_rules] = self._filter_rules()155        self.tc_cmd(tc_conf)156#TODO (ncrao): generate some typical rules: ack, syn, synack,157#              dport/sport, daddr/sddr, etc.158class qdisc(object):159    # tc command160    _tc_cmd = 'tc qdisc %(cmd)s dev %(dev)s %(parent)s ' \161              'handle %(qdiscid)s %(name)s %(params)s'162    def __init__(self, handle):163        self._handle = handle164        self._parent_class = None165        self._tc_conf = None166    def get_handle(self):167        return self._handle168    def get_parent_class(self):169        return self._parent_class170    def set_parent_class(self, parent_class):171        self._parent_class = parent_class172    def _get_tc_conf(self, netif):173        if self._tc_conf:174            return self._tc_conf175        self._tc_conf = dict()176        self._tc_conf[tcfilter.conf_device] = netif.get_name()177        if self._parent_class:178            self._tc_conf[tcfilter.conf_parent] = ('parent %s' %179                                                   self._parent_class.id())180        else:181            self._tc_conf[tcfilter.conf_parent] = 'root'182        self._tc_conf[tcfilter.conf_qdiscid] = self.id()183        self._tc_conf[tcfilter.conf_name] = self.name184        self._tc_conf[tcfilter.conf_params] = ''185        return self._tc_conf186    def id(self):187        return '%s:0' % self._handle188    def tc_cmd(self, tc_conf):189        print self._tc_cmd % tc_conf190    def setup(self, netif):191        tc_conf = self._get_tc_conf(netif)192        tc_conf[tcfilter.conf_command] = 'add'193        self.tc_cmd(tc_conf)194    def restore(self, netif):195        tc_conf = self._get_tc_conf(netif)196        tc_conf[tcfilter.conf_command] = 'del'197        self.tc_cmd(tc_conf)198class classful_qdisc(qdisc):199    classful = True200    def __init__(self, handle):201        super(classful_qdisc, self).__init__(handle)202        self._classes = []203        self._filters = []204    def add_class(self, child_class):205        self._classes.append(child_class)206    def add_filter(self, filter):207        filter.set_parent_qdisc(self)208        self._filters.append(filter)209    def setup(self, netif):210        super(classful_qdisc, self).setup(netif)211        # setup child classes212        for child in self._classes:213            child.setup(netif)214        # setup filters215        for filter in self._filters:216            filter.setup(netif)217    def restore(self, netif):218        # restore filters219        filters_copy = list(self._filters)220        filters_copy.reverse()221        for filter in filters_copy:222            filter.restore(netif)223        # restore child classes224        classes_copy = list(self._classes)225        classes_copy.reverse()226        for child in classes_copy:227            child.restore(netif)228        super(classful_qdisc, self).restore(netif)229class prio(classful_qdisc):230    name = 'prio'231    def __init__(self, handle=new_handle(), bands=3):232        super(prio, self).__init__(handle)233        self._bands = bands234        for counter in range(bands):235            self.add_class(tcclass(handle, counter + 1))236    def setup(self, netif):237        super(prio, self).setup(netif)238    def get_class(self, band):239        if band > self._bands:240            raise error.TestError('error inserting %s at band %s' % \241                                  (qdisc.name, band))242        return self._classes[band]243class classless_qdisc(qdisc):244    classful = False245    def __init__(self, handle):246        super(classless_qdisc, self).__init__(handle)247class pfifo(classless_qdisc):248    name = 'pfifo'249    def __init__(self, handle=new_handle()):250        super(pfifo, self).__init__(handle)251    def setup(self, netif):252        super(pfifo, self).setup(netif)253class netem(classless_qdisc):254    name = 'netem'255    def __init__(self, handle=new_handle()):256        super(netem, self).__init__(handle)257        self._params = list()258    def add_param(self, param):259        self._params.append(param)260    def setup(self, netif):261        super(netem, self).setup(netif)262        tc_conf = self._get_tc_conf(netif)263        tc_conf[tcfilter.conf_command] = 'change'264        tc_conf[tcfilter.conf_params] = ' '.join(self._params)...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!!
