def write(message): output = StringIO() try: amf0.write_int(3, output) # version _write_headers(message.headers, output) _write_bodies(message.bodies, output) return output.getvalue() finally: output.close()
def _write_headers(headers, output): header_count = len(headers) amf0.write_int(header_count, output) for header in headers: context = amf.AMFMessageBodyContext() amf0.write_utf(header['name'], output) amf0.write_byte(header.get('mustUnderstand', False), output) content = StringIO() write_data(header['content'], content, context) amf0.write_long(len(content.getvalue()), output) # Length in bytes of header output.write(content.getvalue()) content.close()
def _write_bodies(bodies, output): body_count = len(bodies) amf0.write_int(body_count, output) for body in bodies: context = amf.AMFMessageBodyContext() amf0.write_utf(body.target, output) if body.response is None or body.response == '': body.response = 'null' amf0.write_utf(body.response, output) content = StringIO() write_data(body.data, content, context) amf0.write_long(len(content.getvalue()), output) # Body length in bytes output.write(content.getvalue()) content.close()