Changeset 184
- Timestamp:
- 07/29/12 20:49:36 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
etherws/trunk/etherws.py
r183 r184 379 379 self._debug = debug 380 380 381 if self._htpasswd:382 self._htpasswd = Htpasswd(self._htpasswd)383 384 381 def get_type(self): 385 382 return 'server' … … 592 589 open(v).close() # check readable 593 590 setattr(ns, k, v) 594 return ns 591 592 def checkpath(ns, path): 593 val = getattr(ns, path, '') 594 if not val.startswith('/'): 595 raise ValueError('invalid %: %s' % (path, val)) 596 597 def getsslopt(ns, key, cert): 598 kval = getattr(ns, key, None) 599 cval = getattr(ns, cert, None) 600 if kval and cval: 601 return {'keyfile': kval, 'certfile': cval} 602 elif kval or cval: 603 raise ValueError('both %s and %s are required' % (key, cert)) 604 return None 605 606 def setport(ns, port, isssl): 607 val = getattr(ns, port, None) 608 if val is None: 609 if isssl: 610 return setattr(ns, port, 443) 611 return setattr(ns, port, 80) 612 if not (0 <= val <= 65535): 613 raise ValueError('invalid %s: %s' % (port, val)) 614 615 def sethtpasswd(ns, htpasswd): 616 val = getattr(ns, htpasswd, None) 617 if val: 618 return setattr(ns, htpasswd, Htpasswd(val)) 595 619 596 620 parser = argparse.ArgumentParser() … … 601 625 602 626 parser.add_argument('--path', action='store', default='/') 603 parser.add_argument('-- address', action='store', default='')627 parser.add_argument('--host', action='store', default='') 604 628 parser.add_argument('--port', action='store', type=int) 605 629 parser.add_argument('--htpasswd', action='store') … … 608 632 609 633 parser.add_argument('--ctlpath', action='store', default='/ctl') 610 parser.add_argument('--ctladdress', action='store', default='127.0.0.1') 611 parser.add_argument('--ctlport', action='store', type=int, default=7867) 612 613 args = realpath(parser.parse_args(), 'htpasswd', 'sslkey', 'sslcert') 634 parser.add_argument('--ctlhost', action='store', default='') 635 parser.add_argument('--ctlport', action='store', type=int) 636 parser.add_argument('--ctlhtpasswd', action='store') 637 parser.add_argument('--ctlsslkey', action='store') 638 parser.add_argument('--ctlsslcert', action='store') 639 640 args = parser.parse_args() 614 641 615 642 #if args.debug: … … 619 646 raise ValueError('invalid ageout: %s' % args.ageout) 620 647 621 if not args.path.startswith('/'): 622 raise ValueError('invalid path: %s' % args.path) 623 624 if not args.ctlpath.startswith('/'): 625 raise ValueError('invalid ctlpath: %s' % args.ctlpath) 626 627 if args.sslkey and args.sslcert: 628 sslopt = {'keyfile': args.sslkey, 'certfile': args.sslcert} 629 elif args.sslkey or args.sslcert: 630 raise ValueError('both sslkey and sslcert are required') 631 else: 632 sslopt = None 633 634 if args.port is None: 635 if sslopt: 636 args.port = 443 637 else: 638 args.port = 80 639 elif not (0 <= args.port <= 65535): 640 raise ValueError('invalid port: %s' % args.port) 641 642 if not (0 <= args.ctlport <= 65535): 643 raise ValueError('invalid ctlport: %s' % args.ctlport) 644 645 if args.htpasswd: 646 args.htpasswd = Htpasswd(args.htpasswd) 648 realpath(args, 'htpasswd', 'sslkey', 'sslcert') 649 realpath(args, 'ctlhtpasswd', 'ctlsslkey', 'ctlsslcert') 650 651 checkpath(args, 'path') 652 checkpath(args, 'ctlpath') 653 654 sslopt = getsslopt(args, 'sslkey', 'sslcert') 655 ctlsslopt = getsslopt(args, 'ctlsslkey', 'ctlsslcert') 656 657 setport(args, 'port', sslopt) 658 setport(args, 'ctlport', ctlsslopt) 659 660 sethtpasswd(args, 'htpasswd') 661 sethtpasswd(args, 'ctlhtpasswd') 647 662 648 663 ioloop = IOLoop.instance() … … 650 665 switch = SwitchingHub(fdb, debug=args.debug) 651 666 652 app = Application([(args.path, EtherWebSocketHandler, { 653 'switch': switch, 654 'htpasswd': args.htpasswd, 655 'debug': args.debug, 656 })]) 657 server = HTTPServer(app, ssl_options=sslopt) 658 server.listen(args.port, address=args.address) 659 660 ctl = Application([(args.ctlpath, EtherWebSocketControlHandler, { 661 'ioloop': ioloop, 662 'switch': switch, 663 'htpasswd': None, 664 'debug': args.debug, 665 })]) 666 ctlserver = HTTPServer(ctl) 667 ctlserver.listen(args.ctlport, address=args.ctladdress) 667 if args.port == args.ctlport and args.host == args.ctlhost: 668 if args.path == args.ctlpath: 669 raise ValueError('same path/ctlpath on same host') 670 if args.sslkey != args.ctlsslkey: 671 raise ValueError('differ sslkey/ctlsslkey on same host') 672 if args.sslcert != args.ctlsslcert: 673 raise ValueError('differ sslcert/ctlsslcert on same host') 674 675 app = Application([ 676 (args.path, EtherWebSocketHandler, { 677 'switch': switch, 678 'htpasswd': args.htpasswd, 679 'debug': args.debug, 680 }), 681 (args.ctlpath, EtherWebSocketControlHandler, { 682 'ioloop': ioloop, 683 'switch': switch, 684 'htpasswd': args.ctlhtpasswd, 685 'debug': args.debug, 686 }), 687 ]) 688 server = HTTPServer(app, ssl_options=sslopt) 689 server.listen(args.port, address=args.host) 690 691 else: 692 app = Application([(args.path, EtherWebSocketHandler, { 693 'switch': switch, 694 'htpasswd': args.htpasswd, 695 'debug': args.debug, 696 })]) 697 server = HTTPServer(app, ssl_options=sslopt) 698 server.listen(args.port, address=args.host) 699 700 ctl = Application([(args.ctlpath, EtherWebSocketControlHandler, { 701 'ioloop': ioloop, 702 'switch': switch, 703 'htpasswd': args.ctlhtpasswd, 704 'debug': args.debug, 705 })]) 706 ctlserver = HTTPServer(ctl, ssl_options=ctlsslopt) 707 ctlserver.listen(args.ctlport, address=args.ctlhost) 668 708 669 709 if not args.foreground:
Note: See TracChangeset
for help on using the changeset viewer.