Esempio n. 1
0
def receive_web_socket_message(socket):
		data = socket.recv(2)
		frame = WebSocketFrame()
		try:
			to_read = frame.handle_header(data[:2], enforce_mask=False)
		except:
			reraise()
		if to_read > 0:
			data = socket.recv(to_read)
			frame.handle_length(data)
		payload = socket.recv(frame.length)
		while len(payload) < frame.length:
			buf = socket.recv(frame.length - len(payload))
			if not buf: raise Exception('Could not read the websocket payload')
			payload += buf
		
		if len(payload) == 0: return None
		return payload
Esempio n. 2
0
	def send(self, message, opcode=WebSocketFrame.OPCODE_TEXT):
		header, message = WebSocketFrame.encode(message, opcode, '1234')
		self.socket.send(header)
		if message: self.socket.send(message)