def connect_targetID(self, targetID): try: wsurl = 'ws://{}:{}/devtools/page/{}'.format( self.host, self.port, targetID) self.close() self.ws = websocket.create_connection(wsurl) self.ws.settimeout(self.timeout) except: wsurl = self.tabs[0]['webSocketDebuggerUrl'] self.ws = websocket.create_connection(wsurl) self.ws.settimeout(self.timeout)
def testWebSocketWihtCustomHeader(self): s = ws.create_connection("ws://echo.websocket.org/", headers={"User-Agent": "PythonWebsocketClient"}) self.assertNotEqual(s, None) s.send("Hello, World") result = s.recv() self.assertEqual(result, "Hello, World") s.close()
def connect(self, tab=0, update_tabs=True): if update_tabs or self.tabs is None: self.get_tabs() self.current_tab = self.tabs[tab] wsurl = self.tabs[tab]['webSocketDebuggerUrl'] self.close() self.ws = websocket.create_connection(wsurl) self.ws.settimeout(self.timeout)
def testWebSocket(self): s = ws.create_connection("ws://echo.websocket.org/") self.assertNotEqual(s, None) s.send("Hello, World") result = s.recv() self.assertEqual(result, "Hello, World") s.send(u"こにゃにゃちは、世界") result = s.recv() self.assertEqual(result, "こにゃにゃちは、世界") s.close()
def testSecureWebSocket(self): if 1: import ssl s = ws.create_connection("wss://echo.websocket.org/") self.assertNotEqual(s, None) self.assertTrue(isinstance(s.sock, ssl.SSLSocket)) s.send("Hello, World") result = s.recv() self.assertEqual(result, "Hello, World") s.send(u"こにゃにゃちは、世界") result = s.recv() self.assertEqual(result, "こにゃにゃちは、世界") s.close()
def testSockOpt(self): sockopt = ((socket.IPPROTO_TCP, socket.TCP_NODELAY, 1),) s = ws.create_connection("ws://echo.websocket.org", sockopt=sockopt) self.assertNotEqual(s.sock.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY), 0) s.close()
def testAfterClose(self): s = ws.create_connection("ws://echo.websocket.org/") self.assertNotEqual(s, None) s.close() self.assertRaises(ws.WebSocketConnectionClosedException, s.send, "Hello") self.assertRaises(ws.WebSocketConnectionClosedException, s.recv)
def testPingPong(self): s = ws.create_connection("ws://echo.websocket.org/") self.assertNotEqual(s, None) s.ping("Hello") s.pong("Hi") s.close()
def testNext(self): sock = ws.create_connection('ws://stream.meetup.com/2/rsvps') self.assertEqual(str, type(next(sock)))
def testIter(self): count = 2 for _ in ws.create_connection('ws://stream.meetup.com/2/rsvps'): count -= 1 if count == 0: break