Changeset 151
- Timestamp:
- 05/16/12 08:56:50 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
etherws/trunk/etherws.py
r150 r151 11 11 # 12 12 # todo: 13 # - authentication support14 13 # - servant mode support (like typical p2p software) 15 14 # … … 44 43 import os 45 44 import sys 46 import time47 45 import base64 48 46 import hashlib 47 import getpass 49 48 import argparse 50 49 import threading … … 112 111 def on_close(self): 113 112 self._tap.unregister_client(self) 113 114 115 class EtherWebSocketClient(object): 116 def __init__(self, tap, url, user=None, passwd=None): 117 self._sock = None 118 self._tap = tap 119 self._url = url 120 self._options = {} 121 122 if user and passwd: 123 token = base64.b64encode('%s:%s' % (user, passwd)) 124 auth = ['Authorization: Basic %s' % token] 125 self._options['header'] = auth 126 127 def open(self): 128 self._sock = websocket.WebSocket() 129 self._sock.connect(self._url, **self._options) 130 131 def close(self): 132 self._sock.close() 133 self._sock = None 134 135 def write_message(self, message, binary=False): 136 flag = websocket.ABNF.OPCODE_TEXT 137 if binary: 138 flag = websocket.ABNF.OPCODE_BINARY 139 self._sock.send(message, flag) 140 141 def run_forever(self): 142 try: 143 if not self._sock: 144 self.open() 145 while True: 146 data = self._sock.recv() 147 if data is None: 148 break 149 self._tap.write(self, data) 150 finally: 151 self.close() 114 152 115 153 … … 214 252 args.port = 80 215 253 216 if not args.foreground:217 daemonize()218 219 254 tap = TapHandler(args.device, debug=args.debug) 220 255 app = tornado.web.Application([ … … 226 261 ioloop = tornado.ioloop.IOLoop.instance() 227 262 ioloop.add_handler(tap.fileno(), tap, ioloop.READ) 263 264 if not args.foreground: 265 daemonize() 266 228 267 ioloop.start() 229 268 … … 233 272 websocket.enableTrace(True) 234 273 274 passwd = None 275 if args.user: 276 passwd = getpass.getpass() 277 278 tap = TapHandler(args.device, debug=args.debug) 279 client = EtherWebSocketClient(tap, args.uri, args.user, passwd) 280 281 tap.register_client(client) 282 client.open() 283 284 t = threading.Thread(target=client.run_forever) 285 t.setDaemon(True) 286 287 ioloop = tornado.ioloop.IOLoop.instance() 288 ioloop.add_handler(tap.fileno(), tap, ioloop.READ) 289 235 290 if not args.foreground: 236 291 daemonize() 237 292 238 tap = TapHandler(args.device, debug=args.debug)239 client = websocket.WebSocketApp(args.uri)240 client.on_message = lambda s, m: tap.write(client, m)241 client.write_message = \242 lambda m, b: client.sock.send(m, websocket.ABNF.OPCODE_BINARY)243 tap.register_client(client)244 245 t = threading.Thread(target=client.run_forever)246 t.setDaemon(True)247 293 t.start() 248 249 while not client.sock:250 time.sleep(0.1)251 252 ioloop = tornado.ioloop.IOLoop.instance()253 ioloop.add_handler(tap.fileno(), tap, ioloop.READ)254 294 ioloop.start() 255 295 … … 273 313 parser_client = subparsers.add_parser('client') 274 314 parser_client.add_argument('--uri', action='store', required=True) 315 parser_client.add_argument('--user', action='store') 275 316 276 317 args = parser.parse_args()
Note: See TracChangeset
for help on using the changeset viewer.