Changeset 190 for etherws/trunk
- Timestamp:
- 07/30/12 21:55:40 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
etherws/trunk/etherws.py
r189 r190 45 45 import fcntl 46 46 import base64 47 import urllib2 47 48 import hashlib 48 49 import getpass … … 527 528 except Exception as e: 528 529 traceback.print_exc() 529 self.finish({'result': None, 'error': str(e), 'id': id_})530 self.finish({'result': None, 'error': {'msg': str(e)}, 'id': id_}) 530 531 531 532 def handle_listPort(self, params): … … 557 558 for p in params: 558 559 port = self._switch.get_port(int(p['port'])) 559 port.shut = bool(p[' flag'])560 port.shut = bool(p['shut']) 560 561 list_.append(self._portstat(port)) 561 562 return {'portlist': list_} … … 584 585 585 586 586 def start_sw itch(args):587 def start_sw(args): 587 588 def daemonize(nochdir=False, noclose=False): 588 589 if os.fork() > 0: … … 718 719 719 720 721 def start_ctl(args): 722 import yaml 723 724 def request(args, method, params): 725 method = '.'.join([EtherWebSocketControlHandler.NAMESPACE, method]) 726 data = json.dumps({'method': method, 'params': params}) 727 req = urllib2.Request(args.ctlurl) 728 req.add_header('Content-type', 'application/json') 729 if args.ctluser: 730 if not args.ctlpasswd: 731 args.ctlpasswd = getpass.getpass() 732 token = base64.b64encode('%s:%s' % (args.ctluser, args.ctlpasswd)) 733 req.add_header('Authorization', 'Basic %s' % token) 734 return json.loads(urllib2.urlopen(req, data).read()) 735 736 def handle_ctl_addport(args): 737 raise NotImplementedError('addport') 738 739 def handle_ctl_shutport(args): 740 if args.port <= 0: 741 raise ValueError('invalid port: %d' % args.port) 742 res = request(args, 'shutPort', [{'port': args.port, 'shut': args.no}]) 743 print(yaml.safe_dump(res)) 744 745 def handle_ctl_delport(args): 746 if args.port <= 0: 747 raise ValueError('invalid port: %d' % args.port) 748 res = request(args, 'delPort', [{'port': args.port}]) 749 print(yaml.safe_dump(res)) 750 751 def handle_ctl_listport(args): 752 res = request(args, 'listPort', []) 753 print(yaml.safe_dump(res)) 754 755 locals()['handle_ctl_' + args.control_method](args) 756 757 720 758 def main(): 721 759 parser = argparse.ArgumentParser() 722 subparsers = parser.add_subparsers(dest='subcommand') 723 parser_s = subparsers.add_parser('switch') 724 parser_c = subparsers.add_parser('control') 760 subcommand = parser.add_subparsers(dest='subcommand') 761 762 # -- sw command parser 763 parser_s = subcommand.add_parser('sw') 725 764 726 765 parser_s.add_argument('--debug', action='store_true', default=False) 727 766 parser_s.add_argument('--foreground', action='store_true', default=False) 728 parser_s.add_argument('--ageout', action='store', type=int, default=300) 729 730 parser_s.add_argument('--path', action='store', default='/') 731 parser_s.add_argument('--host', action='store', default='') 732 parser_s.add_argument('--port', action='store', type=int) 733 parser_s.add_argument('--htpasswd', action='store') 734 parser_s.add_argument('--sslkey', action='store') 735 parser_s.add_argument('--sslcert', action='store') 736 737 parser_s.add_argument('--ctlpath', action='store', default='/ctl') 738 parser_s.add_argument('--ctlhost', action='store', default='') 739 parser_s.add_argument('--ctlport', action='store', type=int) 740 parser_s.add_argument('--ctlhtpasswd', action='store') 741 parser_s.add_argument('--ctlsslkey', action='store') 742 parser_s.add_argument('--ctlsslcert', action='store') 743 767 parser_s.add_argument('--ageout', type=int, default=300) 768 769 parser_s.add_argument('--path', default='/') 770 parser_s.add_argument('--host', default='') 771 parser_s.add_argument('--port', type=int) 772 parser_s.add_argument('--htpasswd') 773 parser_s.add_argument('--sslkey') 774 parser_s.add_argument('--sslcert') 775 776 parser_s.add_argument('--ctlpath', default='/ctl') 777 parser_s.add_argument('--ctlhost', default='') 778 parser_s.add_argument('--ctlport', type=int) 779 parser_s.add_argument('--ctlhtpasswd') 780 parser_s.add_argument('--ctlsslkey') 781 parser_s.add_argument('--ctlsslcert') 782 783 # -- ctl command parser 784 parser_c = subcommand.add_parser('ctl') 785 parser_c.add_argument('--ctlurl', default='http://localhost/ctl') 786 parser_c.add_argument('--ctluser') 787 parser_c.add_argument('--ctlpasswd') 788 789 control_method = parser_c.add_subparsers(dest='control_method') 790 791 parser_c_ap = control_method.add_parser('addport') 792 parser_c_ap.add_argument('target') 793 parser_c_ap.add_argument('--insecure', action='store_true', default=False) 794 parser_c_ap.add_argument('--cacerts') 795 parser_c_ap.add_argument('--user') 796 parser_c_ap.add_argument('--passwd') 797 798 parser_c_sp = control_method.add_parser('shutport') 799 parser_c_sp.add_argument('port', type=int) 800 parser_c_sp.add_argument('--no', action='store_false', default=True) 801 802 parser_c_dp = control_method.add_parser('delport') 803 parser_c_dp.add_argument('port', type=int) 804 805 parser_c_lp = control_method.add_parser('listport') 806 807 # -- go 744 808 args = parser.parse_args() 745 746 809 globals()['start_' + args.subcommand](args) 747 810
Note: See TracChangeset
for help on using the changeset viewer.