def runReceiver(serverAddr, retryInterval=10, username=None, password=None, testMode=False): WsFeedbackReceiver.serverAddr = serverAddr # go into loop trying to do webSocket connection periodically WsFeedbackReceiver.shouldExit = False while not WsFeedbackReceiver.shouldExit: try: if WsFeedbackReceiver.needLogin or WsFeedbackReceiver.sessionCookie is None: WsFeedbackReceiver.sessionCookie = login(serverAddr, username, password, testMode=testMode) wsAddr = os.path.join('wss://', serverAddr, 'wsSubject') if testMode: print("Warning: using non-encrypted connection for test mode") wsAddr = os.path.join('ws://', serverAddr, 'wsSubject') logging.log(DebugLevels.L6, "Trying connection: %s", wsAddr) ws = websocket.WebSocketApp(wsAddr, on_message=WsFeedbackReceiver.on_message, on_close=WsFeedbackReceiver.on_close, on_error=WsFeedbackReceiver.on_error, cookie="login="******"Connected to: %s", wsAddr) print("Connected to: {}".format(wsAddr)) ws.run_forever(sslopt={"ca_certs": certFile}) except Exception as err: logging.log(logging.INFO, "WsFeedbackReceiver Exception {}: {}".format(type(err).__name__, str(err))) print('sleep {}'.format(retryInterval)) time.sleep(retryInterval)
def connectWebClient(): sessionCookie = login(f'localhost:{testPort}', 'test', 'test', testMode=True) ws = websocket.WebSocket() ws.connect(f'ws://localhost:{testPort}/wsUser', cookie="login=" + sessionCookie) return ws
def runFileWatcher(serverAddr, retryInterval=10, allowedDirs=defaultAllowedDirs, allowedTypes=defaultAllowedTypes, username=None, password=None, testMode=False): WsFileWatcher.serverAddr = serverAddr WsFileWatcher.allowedDirs = allowedDirs for i in range(len(allowedTypes)): if not allowedTypes[i].startswith('.'): allowedTypes[i] = '.' + allowedTypes[i] WsFileWatcher.allowedTypes = allowedTypes # go into loop trying to do webSocket connection periodically WsFileWatcher.shouldExit = False while not WsFileWatcher.shouldExit: try: if WsFileWatcher.needLogin or WsFileWatcher.sessionCookie is None: WsFileWatcher.sessionCookie = login(serverAddr, username, password, testMode=testMode) wsAddr = os.path.join('wss://', serverAddr, 'wsData') if testMode: print( "Warning: using non-encrypted connection for test mode" ) wsAddr = os.path.join('ws://', serverAddr, 'wsData') logging.log(DebugLevels.L6, "Trying connection: %s", wsAddr) ws = websocket.WebSocketApp( wsAddr, on_message=WsFileWatcher.on_message, on_close=WsFileWatcher.on_close, on_error=WsFileWatcher.on_error, cookie="login="******"Connected to: %s", wsAddr) print("Connected to: {}".format(wsAddr)) ws.run_forever(sslopt={"ca_certs": certFile}) except Exception as err: logging.log( logging.INFO, "WSFileWatcher Exception {}: {}".format( type(err).__name__, str(err))) print('sleep {}'.format(retryInterval)) time.sleep(retryInterval)
def runForever(self): """Run the receiver loop. This function doesn't return.""" # go into loop trying to do webSocket connection periodically args = self.args # print(f'args {self.args}') WsRemoteService.shouldExit = False while not WsRemoteService.shouldExit: try: if self.needLogin or self.sessionCookie is None: self.sessionCookie = login(args.server, args.username, args.password, testMode=args.test) wsAddr = os.path.join('wss://', args.server, self.channelName) if args.test: print( "Warning: using non-encrypted connection for test mode" ) wsAddr = os.path.join('ws://', args.server, self.channelName) logging.log(DebugLevels.L6, "Trying connection: %s", wsAddr) ws = websocket.WebSocketApp( wsAddr, on_message=WsRemoteService.on_message, on_close=WsRemoteService.on_close, on_error=WsRemoteService.on_error, cookie="login="******"Connected to: %s", wsAddr) print("Connected to: {}".format(wsAddr)) self.started = True ws.run_forever(sslopt={"ca_certs": certFile}) except Exception as err: logging.log( logging.INFO, "WsRemoteService Exception {}: {}".format( type(err).__name__, str(err))) print('sleep {}'.format(args.interval)) time.sleep(args.interval)