我们从Python开源项目中,提取了以下49个代码示例,用于说明如何使用websocket.WebSocket()。
def connect(self, host = None, port = None): """ Connect to the websocket server :return: Nothing """ if not host is None: self.host = host if not port is None: self.port = port try: self.ws = websocket.WebSocket() LOG.info("Connecting...") self.ws.connect("ws://{}:{}".format(self.host, self.port)) LOG.info("Connected!") self._auth(self.password) self._run_threads() except socket.error as e: raise exceptions.ConnectionFailure(str(e))
def testInternalRecvStrict(self): sock = ws.WebSocket() s = sock.sock = SockMock() s.add_packet(six.b("foo")) s.add_packet(socket.timeout()) s.add_packet(six.b("bar")) # s.add_packet(SSLError("The read operation timed out")) s.add_packet(six.b("baz")) with self.assertRaises(ws.WebSocketTimeoutException): data = sock.frame_buffer.recv_strict(9) # if six.PY2: # with self.assertRaises(ws.WebSocketTimeoutException): # data = sock._recv_strict(9) # else: # with self.assertRaises(SSLError): # data = sock._recv_strict(9) data = sock.frame_buffer.recv_strict(9) self.assertEqual(data, six.b("foobarbaz")) with self.assertRaises(ws.WebSocketConnectionClosedException): data = sock.frame_buffer.recv_strict(1)
def testRecvTimeout(self): sock = ws.WebSocket() s = sock.sock = SockMock() s.add_packet(six.b("\x81")) s.add_packet(socket.timeout()) s.add_packet(six.b("\x8dabcd\x29\x07\x0f\x08\x0e")) s.add_packet(socket.timeout()) s.add_packet(six.b("\x4e\x43\x33\x0e\x10\x0f\x00\x40")) with self.assertRaises(ws.WebSocketTimeoutException): data = sock.recv() with self.assertRaises(ws.WebSocketTimeoutException): data = sock.recv() data = sock.recv() self.assertEqual(data, "Hello, World!") with self.assertRaises(ws.WebSocketConnectionClosedException): data = sock.recv()
def testRecvWithProlongedFragmentation(self): sock = ws.WebSocket() s = sock.sock = SockMock() # OPCODE=TEXT, FIN=0, MSG="Once more unto the breach, " s.add_packet(six.b("\x01\x9babcd.\x0c\x00\x01A\x0f\x0c\x16\x04B\x16\n\x15" \ "\rC\x10\t\x07C\x06\x13\x07\x02\x07\tNC")) # OPCODE=CONT, FIN=0, MSG="dear friends, " s.add_packet(six.b("\x00\x8eabcd\x05\x07\x02\x16A\x04\x11\r\x04\x0c\x07" \ "\x17MB")) # OPCODE=CONT, FIN=1, MSG="once more" s.add_packet(six.b("\x80\x89abcd\x0e\x0c\x00\x01A\x0f\x0c\x16\x04")) data = sock.recv() self.assertEqual( data, "Once more unto the breach, dear friends, once more") with self.assertRaises(ws.WebSocketConnectionClosedException): sock.recv()
def testRecvWithFragmentationAndControlFrame(self): sock = ws.WebSocket() sock.set_mask_key(create_mask_key) s = sock.sock = SockMock() # OPCODE=TEXT, FIN=0, MSG="Too much " s.add_packet(six.b("\x01\x89abcd5\r\x0cD\x0c\x17\x00\x0cA")) # OPCODE=PING, FIN=1, MSG="Please PONG this" s.add_packet(six.b("\x89\x90abcd1\x0e\x06\x05\x12\x07C4.,$D\x15\n\n\x17")) # OPCODE=CONT, FIN=1, MSG="of a good thing" s.add_packet(six.b("\x80\x8fabcd\x0e\x04C\x05A\x05\x0c\x0b\x05B\x17\x0c" \ "\x08\x0c\x04")) data = sock.recv() self.assertEqual(data, "Too much of a good thing") with self.assertRaises(ws.WebSocketConnectionClosedException): sock.recv() self.assertEqual( s.sent[0], six.b("\x8a\x90abcd1\x0e\x06\x05\x12\x07C4.,$D\x15\n\n\x17"))
def testRecvWithFireEventOfFragmentation(self): sock = ws.WebSocket(fire_cont_frame=True) s = sock.sock = SockMock() # OPCODE=TEXT, FIN=0, MSG="Brevity is " s.add_packet(six.b("\x01\x8babcd#\x10\x06\x12\x08\x16\x1aD\x08\x11C")) # OPCODE=CONT, FIN=0, MSG="Brevity is " s.add_packet(six.b("\x00\x8babcd#\x10\x06\x12\x08\x16\x1aD\x08\x11C")) # OPCODE=CONT, FIN=1, MSG="the soul of wit" s.add_packet(six.b("\x80\x8fabcd\x15\n\x06D\x12\r\x16\x08A\r\x05D\x16\x0b\x17")) _, data = sock.recv_data() self.assertEqual(data, six.b("Brevity is ")) _, data = sock.recv_data() self.assertEqual(data, six.b("Brevity is ")) _, data = sock.recv_data() self.assertEqual(data, six.b("the soul of wit")) # OPCODE=CONT, FIN=0, MSG="Brevity is " s.add_packet(six.b("\x80\x8babcd#\x10\x06\x12\x08\x16\x1aD\x08\x11C")) with self.assertRaises(ws.WebSocketException): sock.recv_data() with self.assertRaises(ws.WebSocketConnectionClosedException): sock.recv()
def testInternalRecvStrict(self): sock = ws.WebSocket() s = sock.sock = SockMock() s.add_packet(six.b("foo")) s.add_packet(socket.timeout()) s.add_packet(six.b("bar")) # s.add_packet(SSLError("The read operation timed out")) s.add_packet(six.b("baz")) with self.assertRaises(ws.WebSocketTimeoutException): sock.frame_buffer.recv_strict(9) # if six.PY2: # with self.assertRaises(ws.WebSocketTimeoutException): # data = sock._recv_strict(9) # else: # with self.assertRaises(SSLError): # data = sock._recv_strict(9) data = sock.frame_buffer.recv_strict(9) self.assertEqual(data, six.b("foobarbaz")) with self.assertRaises(ws.WebSocketConnectionClosedException): sock.frame_buffer.recv_strict(1)
def testRecvTimeout(self): sock = ws.WebSocket() s = sock.sock = SockMock() s.add_packet(six.b("\x81")) s.add_packet(socket.timeout()) s.add_packet(six.b("\x8dabcd\x29\x07\x0f\x08\x0e")) s.add_packet(socket.timeout()) s.add_packet(six.b("\x4e\x43\x33\x0e\x10\x0f\x00\x40")) with self.assertRaises(ws.WebSocketTimeoutException): sock.recv() with self.assertRaises(ws.WebSocketTimeoutException): sock.recv() data = sock.recv() self.assertEqual(data, "Hello, World!") with self.assertRaises(ws.WebSocketConnectionClosedException): sock.recv()
def testRecvWithProlongedFragmentation(self): sock = ws.WebSocket() s = sock.sock = SockMock() # OPCODE=TEXT, FIN=0, MSG="Once more unto the breach, " s.add_packet(six.b("\x01\x9babcd.\x0c\x00\x01A\x0f\x0c\x16\x04B\x16\n\x15" "\rC\x10\t\x07C\x06\x13\x07\x02\x07\tNC")) # OPCODE=CONT, FIN=0, MSG="dear friends, " s.add_packet(six.b("\x00\x8eabcd\x05\x07\x02\x16A\x04\x11\r\x04\x0c\x07" "\x17MB")) # OPCODE=CONT, FIN=1, MSG="once more" s.add_packet(six.b("\x80\x89abcd\x0e\x0c\x00\x01A\x0f\x0c\x16\x04")) data = sock.recv() self.assertEqual( data, "Once more unto the breach, dear friends, once more") with self.assertRaises(ws.WebSocketConnectionClosedException): sock.recv()
def testRecvWithFragmentationAndControlFrame(self): sock = ws.WebSocket() sock.set_mask_key(create_mask_key) s = sock.sock = SockMock() # OPCODE=TEXT, FIN=0, MSG="Too much " s.add_packet(six.b("\x01\x89abcd5\r\x0cD\x0c\x17\x00\x0cA")) # OPCODE=PING, FIN=1, MSG="Please PONG this" s.add_packet(six.b("\x89\x90abcd1\x0e\x06\x05\x12\x07C4.,$D\x15\n\n\x17")) # OPCODE=CONT, FIN=1, MSG="of a good thing" s.add_packet(six.b("\x80\x8fabcd\x0e\x04C\x05A\x05\x0c\x0b\x05B\x17\x0c" "\x08\x0c\x04")) data = sock.recv() self.assertEqual(data, "Too much of a good thing") with self.assertRaises(ws.WebSocketConnectionClosedException): sock.recv() self.assertEqual( s.sent[0], six.b("\x8a\x90abcd1\x0e\x06\x05\x12\x07C4.,$D\x15\n\n\x17"))
def recv(self): chunk = b"" if type(self.connection) is websocket.WebSocket: chunk = self.connection.recv() else: incomplete = True while incomplete: chunk += self.connection.recv(self.chunk_size) try: chunk = chunk.decode('utf8') incomplete = False except: pass return chunk