仅取出包含实际正文结构的服务器答案的内部部分:
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") '
'"<538@goomoji.gmail>" NIL "BASE64" 172)("IMAGE" "PNG" ("NAME" '
'"4F4.png") "<gtalk.4F4@goomoji.gmail>" NIL "BASE64" 754) "RELATED")'
'("IMAGE" "JPEG" ("NAME" "avatar_airbender.jpg") NIL NIL "BASE64"'
' 157924) "MIXED")')
下一步是替换一些标记,将准备字符串转换为 python 类型:
struct = struct.replace(' ', ',').replace(')(', '),(')
使用内置模块编译器解析我们的结构:
import compiler
expr = compiler.parse(struct.replace(' ', ',').replace(')(', '),('), 'eval')
执行简单的递归函数来转换表达式:
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
最后我们得到期望的结果作为嵌套的 python 元组:
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'), '<538@goomoji.gmail>', None, 'BASE64', 172), ('IMAGE', 'PNG', ('NAME', '4F4.png'), '<gtalk.4F4@goomoji.gmail>', None, 'BASE64', 754), 'RELATED'), ('IMAGE', 'JPEG', ('NAME', 'avatar_airbender.jpg'), None, None, 'BASE64', 157924), 'MIXED')
从那里我们可以识别不同的主体结构标题:
text, attachments = (result[0], result[1:])