<div dir="ltr"><br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

<div dir="ltr"><div><div><div><div><div><div>I have this snippet of borrowed code. <br>
<br>def showPayload(msg):<br>    payload = msg.get_payload()<br><br>    if msg.is_multipart():<br>        div = &#39;&#39;<br>        for subMsg in payload:<br>            print div<br>            showPayload(subMsg)<br>

            div = &#39;------------------------------&#39;<br>
    else:<br>        print msg.get_content_type()<br>        print payload[:200]<br></div></div></div></div></div></div></div></blockquote><div><br></div><div><br></div><div>Jim was definitely on the right track with the &quot;file-like object&quot; idea, but instead of crafting one yourself, as Jim did with Buffer, there is a built-in string file-like object called StringIO.</div>

<div><br></div><div>So right now you are using the &quot;print&quot; statement which is a convenience keyword (removed in Python 3) to the built-in print function[1], which passes in sys.stdout, which is a built-in file-like object to standard out. So your last line:</div>

<div><br></div><div>print payload[:200]</div><div><br></div><div>is equivalent to:</div><div><br></div><div>import sys</div><div>sys.stdout.writeline(payload[:200] + &#39;\n&#39;)</div><div><br></div><div>Because of the recursive nature of the showPayload function, you can&#39;t create the file-like object within the function (just like sys.stdout isn&#39;t created within the function). So you will need to pass in the file like object, and if you want to maintain existing behavior, default to sys.stdout.  So your function signature becomes:</div>

<div><br></div><div>def showPayload(msg, outfile=sys.stdout)</div><div><br></div><div>And then your print statements become &#39;outfile.write()&#39; calls, so the entire method looks like:</div><div><br></div><div><br></div>

<div>import sys</div><div><br></div><div><span style="font-family:arial,sans-serif;font-size:12.800000190734863px">def showPayload(msg, outfile=sys.stdout):</span><br style="font-family:arial,sans-serif;font-size:12.800000190734863px">

<span style="font-family:arial,sans-serif;font-size:12.800000190734863px">    payload = msg.get_payload()</span><br style="font-family:arial,sans-serif;font-size:12.800000190734863px"><br style="font-family:arial,sans-serif;font-size:12.800000190734863px">

<span style="font-family:arial,sans-serif;font-size:12.800000190734863px">    if msg.is_multipart():</span><br style="font-family:arial,sans-serif;font-size:12.800000190734863px"><span style="font-family:arial,sans-serif;font-size:12.800000190734863px">        div = &#39;&#39;</span><br style="font-family:arial,sans-serif;font-size:12.800000190734863px">

<span style="font-family:arial,sans-serif;font-size:12.800000190734863px">        for subMsg in payload:</span><br style="font-family:arial,sans-serif;font-size:12.800000190734863px"><span style="font-family:arial,sans-serif;font-size:12.800000190734863px">            outfile.write(div + &#39;\n&#39;)</span><br style="font-family:arial,sans-serif;font-size:12.800000190734863px">

<span style="font-family:arial,sans-serif;font-size:12.800000190734863px">            showPayload(subMsg, outfile)</span><br style="font-family:arial,sans-serif;font-size:12.800000190734863px"><span style="font-family:arial,sans-serif;font-size:12.800000190734863px">            div = &#39;-----------------------------</span><span style="font-family:arial,sans-serif;font-size:12.800000190734863px">-&#39;</span><br style="font-family:arial,sans-serif;font-size:12.800000190734863px">

<span style="font-family:arial,sans-serif;font-size:12.800000190734863px">    else:</span><br style="font-family:arial,sans-serif;font-size:12.800000190734863px"><span style="font-family:arial,sans-serif;font-size:12.800000190734863px">        outfile.write(msg.get_content_type() + &#39;\n&#39;)</span><br style="font-family:arial,sans-serif;font-size:12.800000190734863px">

<span style="font-family:arial,sans-serif;font-size:12.800000190734863px">        outfile.write(payload[:200] + &#39;\n&#39;)</span><br style="font-family:arial,sans-serif;font-size:12.800000190734863px"></div><div><br></div>

<div><br></div><div>So now you can use it the same way as you do now (i.e. showPayload(msg)) which will print to standard out, or you can pass in a file like object, like a file on disk, or a string file, like:</div><div>

<br></div><div>from StringIO import StringIO</div><div><br></div><div>stringfile = StringIO()</div><div><br></div><div>showPayload(msg, stringfile)</div><div><br></div><div>stringfile.getvalue() # Contents of string</div>

<div><br></div><div><br></div><div>Or a regular file:</div><div><br></div><div>diskfile = open(&#39;/tmp/myfile.txt&#39;, &#39;w&#39;)</div><div>showPayload(msg, diskfile)</div><div>diskfile.close()</div><div><br></div><div>

<br></div><div>[1] <a href="http://docs.python.org/2/library/functions.html#print">http://docs.python.org/2/library/functions.html#print</a>  -- One note, &quot;print&quot; is an area that is changing a bit between Python 2 and Python 3. Don&#39;t get into the bad habit of using, say, the chevron version of print to redirect, as it&#39;s going away. In Python 3 you&#39;ll use this print function built-in instead.<br>

</div><div><br></div></div></div></div>