def testStreaming(self): bin_data = os.urandom(20) f = Msgpack.FilePart("%s/users.json" % config.data_dir, "rb") f.read_bytes = 30 data = {"cmd": "response", "body": f, "bin": bin_data} out_buff = io.BytesIO() Msgpack.stream(data, out_buff.write) out_buff.seek(0) data_packb = { "cmd": "response", "body": open("%s/users.json" % config.data_dir, "rb").read(30), "bin": bin_data } out_buff.seek(0) data_unpacked = Msgpack.unpack(out_buff.read()) assert data_unpacked == data_packb assert data_unpacked["cmd"] == "response" assert type(data_unpacked["body"]) == bytes
def send(self, message, streaming=False): self.last_send_time = time.time() if config.debug_socket: self.log( "Send: %s, to: %s, streaming: %s, site: %s, inner_path: %s, req_id: %s" % (message.get("cmd"), message.get("to"), streaming, message.get("params", {}).get("site"), message.get("params", {}).get("inner_path"), message.get("req_id"))) if not self.sock: self.log("Send error: missing socket") return False if not self.connected and message.get("cmd") != "handshake": self.log("Wait for handshake before send request") self.event_connected.get() try: stat_key = message.get("cmd", "unknown") if stat_key == "response": stat_key = "response: %s" % self.last_cmd_recv else: self.server.num_sent += 1 self.server.stat_sent[stat_key]["num"] += 1 if streaming: with self.send_lock: bytes_sent = Msgpack.stream(message, self.sock.sendall) self.bytes_sent += bytes_sent self.server.bytes_sent += bytes_sent self.server.stat_sent[stat_key]["bytes"] += bytes_sent message = None else: data = Msgpack.pack(message) self.bytes_sent += len(data) self.server.bytes_sent += len(data) self.server.stat_sent[stat_key]["bytes"] += len(data) message = None with self.send_lock: self.sock.sendall(data) except Exception as err: self.close("Send error: %s (cmd: %s)" % (err, stat_key)) return False self.last_sent_time = time.time() return True