Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Taking out only internal part of the server answer containing actualy the body structure:</p> <pre><code>struct = ('(((("TEXT" "PLAIN" ("CHARSET" "ISO-8859-1") NIL NIL "7BIT" 16 2)' '("TEXT" "HTML" ("CHARSET" "ISO-8859-1") NIL NIL "QUOTED-PRINTABLE"' ' 392 6) "ALTERNATIVE")("IMAGE" "GIF" ("NAME" "538.gif") ' '"&lt;538@goomoji.gmail&gt;" NIL "BASE64" 172)("IMAGE" "PNG" ("NAME" ' '"4F4.png") "&lt;gtalk.4F4@goomoji.gmail&gt;" NIL "BASE64" 754) "RELATED")' '("IMAGE" "JPEG" ("NAME" "avatar_airbender.jpg") NIL NIL "BASE64"' ' 157924) "MIXED")') </code></pre> <p>Next step is to replace some tokens, what would prepair string to transform into python types: </p> <pre><code>struct = struct.replace(' ', ',').replace(')(', '),(') </code></pre> <p>Using built-in module <a href="http://docs.python.org/library/compiler.html" rel="nofollow">compiler</a> to parse our structure:</p> <pre><code>import compiler expr = compiler.parse(struct.replace(' ', ',').replace(')(', '),('), 'eval') </code></pre> <p>Performing simple recursive function to transform expression:</p> <pre><code>def transform(expression): if isinstance(expression, compiler.transformer.Expression): return transform(expression.node) elif isinstance(expression, compiler.transformer.Tuple): return tuple(transform(item) for item in expression.nodes) elif isinstance(expression, compiler.transformer.Const): return expression.value elif isinstance(expression, compiler.transformer.Name): return None if expression.name == 'NIL' else expression.name </code></pre> <p>And finally we get the desired result as nested python tuples:</p> <pre><code>result = transform(expr) print result (((('TEXT', 'PLAIN', ('CHARSET', 'ISO-8859-1'), None, None, '7BIT', 16, 2), ('TEXT', 'HTML', ('CHARSET', 'ISO-8859-1'), None, None, 'QUOTED-PRINTABLE', 392, 6), 'ALTERNATIVE'), ('IMAGE', 'GIF', ('NAME', '538.gif'), '&lt;538@goomoji.gmail&gt;', None, 'BASE64', 172), ('IMAGE', 'PNG', ('NAME', '4F4.png'), '&lt;gtalk.4F4@goomoji.gmail&gt;', None, 'BASE64', 754), 'RELATED'), ('IMAGE', 'JPEG', ('NAME', 'avatar_airbender.jpg'), None, None, 'BASE64', 157924), 'MIXED') </code></pre> <p>From where we can recognize different headers of body structure:</p> <pre><code>text, attachments = (result[0], result[1:]) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload