Changeset 199
- Timestamp:
- 08/01/12 02:16:57 (12 years ago)
- Location:
- etherws/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
etherws/trunk/etherws.py
r198 r199 9 9 # - websocket-client-0.7.0 10 10 # - tornado-2.3 11 # - PyYAML-3.1012 11 # 13 12 # =========================================================================== … … 44 43 import time 45 44 import json 46 import yaml47 45 import fcntl 48 46 import base64 … … 559 557 if entry: 560 558 mac = EthernetFrame.format_mac(mac) 561 list_.append([vid, mac, entry.port.number, int(entry.age)]) 562 return {'result': list_} 559 list_.append({ 560 'vid': vid, 561 'mac': mac, 562 'port': entry.port.number, 563 'age': int(entry.age), 564 }) 565 return {'entries': list_} 563 566 564 567 def handle_listPort(self, params): 565 568 list_ = [self._portstat(p) for p in self._switch.portlist] 566 return {' result': list_}569 return {'entries': list_} 567 570 568 571 def handle_addPort(self, params): … … 576 579 portnum = interface.open() 577 580 list_.append(self._portstat(self._switch.get_port(portnum))) 578 return {' result': list_}581 return {'entries': list_} 579 582 580 583 def handle_delPort(self, params): … … 584 587 list_.append(self._portstat(port)) 585 588 port.interface.close() 586 return {' result': list_}589 return {'entries': list_} 587 590 588 591 def handle_shutPort(self, params): … … 592 595 port.shut = bool(p['shut']) 593 596 list_.append(self._portstat(port)) 594 return {' result': list_}597 return {'entries': list_} 595 598 596 599 def _optparse_tap(self, opt): … … 764 767 return json.loads(urllib2.urlopen(req, data).read()) 765 768 769 def maxlen(dict_, key, min_): 770 max_ = max(len(str(r[key])) for r in dict_) 771 return min_ if max_ < min_ else max_ 772 773 def print_portlist(result): 774 pmax = maxlen(result, 'port', 4) 775 ymax = maxlen(result, 'type', 4) 776 smax = maxlen(result, 'shut', 5) 777 rmax = maxlen(result, 'rx', 2) 778 tmax = maxlen(result, 'tx', 2) 779 fmt = ' %%%ds %%%ds %%%ds %%%ds %%%ds %%s' % \ 780 (pmax, ymax, smax, rmax, tmax) 781 print(fmt % ('Port', 'Type', 'State', 'RX', 'TX', 'Target')) 782 for r in result: 783 shut = 'shut' if r['shut'] else 'up' 784 print(fmt % 785 (r['port'], r['type'], shut, r['rx'], r['tx'], r['target'])) 786 766 787 def handle_ctl_addport(args): 767 788 params = [{ … … 775 796 } 776 797 }] 777 return request(args, 'addPort', params) 798 result = request(args, 'addPort', params) 799 if result['error']: 800 print(result['error']['message']) 801 else: 802 print_portlist(result['result']['entries']) 778 803 779 804 def handle_ctl_shutport(args): … … 781 806 raise ValueError('invalid port: %d' % args.port) 782 807 params = [{'port': args.port, 'shut': args.no}] 783 return request(args, 'shutPort', params) 808 result = request(args, 'shutPort', params) 809 if result['error']: 810 print(result['error']['message']) 811 else: 812 print_portlist(result['result']['entries']) 784 813 785 814 def handle_ctl_delport(args): … … 787 816 raise ValueError('invalid port: %d' % args.port) 788 817 params = [{'port': args.port}] 789 return request(args, 'delPort', params) 818 result = request(args, 'delPort', params) 819 if result['error']: 820 print(result['error']['message']) 821 else: 822 print_portlist(result['result']['entries']) 790 823 791 824 def handle_ctl_listport(args): 792 return request(args, 'listPort', []) 825 result = request(args, 'listPort', []) 826 if result['error']: 827 print(result['error']['message']) 828 else: 829 print_portlist(result['result']['entries']) 793 830 794 831 def handle_ctl_listfdb(args): 795 return request(args, 'listFdb', []) 796 797 res = locals()['handle_ctl_' + args.control_method](args) 798 799 if res['error']: 800 print(res['error']['message']) 801 else: 802 print(yaml.safe_dump(res['result']).strip()) 832 result = request(args, 'listFdb', []) 833 if result['error']: 834 print(result['error']['message']) 835 return 836 result = result['result']['entries'] 837 vmax = maxlen(result, 'vid', 4) 838 mmax = maxlen(result, 'mac', 3) 839 pmax = maxlen(result, 'port', 4) 840 amax = maxlen(result, 'age', 3) 841 fmt = ' %%%ds %%%ds %%%ds %%%ds' % (vmax, mmax, pmax, amax) 842 print(fmt % ('VLAN', 'MAC', 'Port', 'Age')) 843 for r in result: 844 print(fmt % (r['vid'], r['mac'], r['port'], r['age'])) 845 846 locals()['handle_ctl_' + args.control_method](args) 803 847 804 848 -
etherws/trunk/setup.py
r194 r199 51 51 'websocket-client>=0.7.0', 52 52 'tornado>=2.3', 53 'PyYAML>=3.10',54 53 ], 55 54 classifiers=[
Note: See TracChangeset
for help on using the changeset viewer.