<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Handling multipart/form-data in Python</title>
	<atom:link href="http://stacyprowell.com/blog/2009/05/29/handling-multipartform-data-in-python/feed/" rel="self" type="application/rss+xml" />
	<link>http://stacyprowell.com/blog/2009/05/29/handling-multipartform-data-in-python/</link>
	<description>Ugh, Stacy&#039;s talking again...</description>
	<lastBuildDate>Thu, 19 Apr 2012 14:17:04 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
	<item>
		<title>By: pete</title>
		<link>http://stacyprowell.com/blog/2009/05/29/handling-multipartform-data-in-python/comment-page-1/#comment-174</link>
		<dc:creator>pete</dc:creator>
		<pubDate>Thu, 10 Nov 2011 18:55:29 +0000</pubDate>
		<guid isPermaLink="false">http://stacyprowell.com/blog/?p=225#comment-174</guid>
		<description>Yes, yes, and yes!  I&#039;ve been scouring the web for a simple way to upload a simple file, and this is it.  Many thanks.</description>
		<content:encoded><![CDATA[<p>Yes, yes, and yes!  I&#8217;ve been scouring the web for a simple way to upload a simple file, and this is it.  Many thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bruzed</title>
		<link>http://stacyprowell.com/blog/2009/05/29/handling-multipartform-data-in-python/comment-page-1/#comment-166</link>
		<dc:creator>bruzed</dc:creator>
		<pubDate>Fri, 14 Jan 2011 18:33:46 +0000</pubDate>
		<guid isPermaLink="false">http://stacyprowell.com/blog/?p=225#comment-166</guid>
		<description>hi, is there a reason why this might not work on python under windows? it looks like it doesn&#039;t write all the parts. just wanted to check if anyone faced something similar. thanks!</description>
		<content:encoded><![CDATA[<p>hi, is there a reason why this might not work on python under windows? it looks like it doesn&#8217;t write all the parts. just wanted to check if anyone faced something similar. thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stacy</title>
		<link>http://stacyprowell.com/blog/2009/05/29/handling-multipartform-data-in-python/comment-page-1/#comment-123</link>
		<dc:creator>stacy</dc:creator>
		<pubDate>Wed, 12 May 2010 18:09:05 +0000</pubDate>
		<guid isPermaLink="false">http://stacyprowell.com/blog/?p=225#comment-123</guid>
		<description>demikaze: I add it because of the python formatter / processor I use.  It makes it work correctly, and it also causes the Eclipse environment to correctly indent, etc.  In short, yes, it is needed (by me, not the interpreter).</description>
		<content:encoded><![CDATA[<p>demikaze: I add it because of the python formatter / processor I use.  It makes it work correctly, and it also causes the Eclipse environment to correctly indent, etc.  In short, yes, it is needed (by me, not the interpreter).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: demikaze</title>
		<link>http://stacyprowell.com/blog/2009/05/29/handling-multipartform-data-in-python/comment-page-1/#comment-118</link>
		<dc:creator>demikaze</dc:creator>
		<pubDate>Wed, 12 May 2010 11:45:34 +0000</pubDate>
		<guid isPermaLink="false">http://stacyprowell.com/blog/?p=225#comment-118</guid>
		<description>&#039;return&#039; without parameters is not needed ;)</description>
		<content:encoded><![CDATA[<p>&#8216;return&#8217; without parameters is not needed <img src='http://stacyprowell.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Larry Henrie</title>
		<link>http://stacyprowell.com/blog/2009/05/29/handling-multipartform-data-in-python/comment-page-1/#comment-101</link>
		<dc:creator>Larry Henrie</dc:creator>
		<pubDate>Sat, 06 Mar 2010 23:33:48 +0000</pubDate>
		<guid isPermaLink="false">http://stacyprowell.com/blog/?p=225#comment-101</guid>
		<description>Cool Thanks for this blog. I am new at development and this is a big help.</description>
		<content:encoded><![CDATA[<p>Cool Thanks for this blog. I am new at development and this is a big help.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Raphaël Londeix</title>
		<link>http://stacyprowell.com/blog/2009/05/29/handling-multipartform-data-in-python/comment-page-1/#comment-61</link>
		<dc:creator>Raphaël Londeix</dc:creator>
		<pubDate>Fri, 14 Aug 2009 13:17:34 +0000</pubDate>
		<guid isPermaLink="false">http://stacyprowell.com/blog/?p=225#comment-61</guid>
		<description>[code]
#-*- coding: utf-8 -*-

from hashlib import sha256
import random
import time

CRLF = &#039;\r\n&#039;

class MultipartResponse(object):

    def __init__(self, multipart=&#039;mixed&#039;, boundary=None):
        if boundary is None:
            random.seed(time.time())
            rand = random.randint(1000,999999999)
            self.boundary = sha256(str(rand)).hexdigest()
        self.content_type = &#039;multipart/&#039;+multipart+&#039;; boundary=%s&#039; % self.boundary
        self.parts = []


    def _addPart(self, data, headers):
        part = [&#039;--&#039; + self.boundary] + headers + [&#039;&#039;] + [data]
        self.parts.append(CRLF.join(part))

    def addText(self, text, charset=&quot;utf-8&quot;):
        content_type = &quot;Content-Type: text/plain; charset=%s&quot; % charset
        self._addPart(text, [content_type])

    def addHTML(self, html, charset=&quot;utf-8&quot;):
        content_type = &quot;Content-Type: text/html; charset=%s&quot; % charset
        self._addPart(html, [content_type])

    def addFile(self, data, mime_type, filename, charset=&quot;utf-8&quot;):
        content_type = &quot;Content-Type: %s; charset=%s&quot; % (mime_type, charset)
        content_disposition = &quot;Content-disposition: attachment; filename=%s&quot; % filename
        self._addPart(data, [content_type, content_disposition])

    def render(self):
        all = self.parts + [&#039;--&#039; + self.boundary + &#039;--&#039;] + [&#039;&#039;]
        return CRLF.join(all)
[code]</description>
		<content:encoded><![CDATA[<p>[code]<br />
#-*- coding: utf-8 -*-</p>
<p>from hashlib import sha256<br />
import random<br />
import time</p>
<p>CRLF = '\r\n'</p>
<p>class MultipartResponse(object):</p>
<p>    def __init__(self, multipart='mixed', boundary=None):<br />
        if boundary is None:<br />
            random.seed(time.time())<br />
            rand = random.randint(1000,999999999)<br />
            self.boundary = sha256(str(rand)).hexdigest()<br />
        self.content_type = 'multipart/'+multipart+'; boundary=%s' % self.boundary<br />
        self.parts = []</p>
<p>    def _addPart(self, data, headers):<br />
        part = ['--' + self.boundary] + headers + [''] + [data]<br />
        self.parts.append(CRLF.join(part))</p>
<p>    def addText(self, text, charset="utf-8"):<br />
        content_type = "Content-Type: text/plain; charset=%s" % charset<br />
        self._addPart(text, [content_type])</p>
<p>    def addHTML(self, html, charset="utf-8"):<br />
        content_type = "Content-Type: text/html; charset=%s" % charset<br />
        self._addPart(html, [content_type])</p>
<p>    def addFile(self, data, mime_type, filename, charset="utf-8"):<br />
        content_type = "Content-Type: %s; charset=%s" % (mime_type, charset)<br />
        content_disposition = "Content-disposition: attachment; filename=%s" % filename<br />
        self._addPart(data, [content_type, content_disposition])</p>
<p>    def render(self):<br />
        all = self.parts + ['--' + self.boundary + '--'] + ['']<br />
        return CRLF.join(all)<br />
[code]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Raphaël Londeix</title>
		<link>http://stacyprowell.com/blog/2009/05/29/handling-multipartform-data-in-python/comment-page-1/#comment-60</link>
		<dc:creator>Raphaël Londeix</dc:creator>
		<pubDate>Fri, 14 Aug 2009 13:16:47 +0000</pubDate>
		<guid isPermaLink="false">http://stacyprowell.com/blog/?p=225#comment-60</guid>
		<description>Hi, your post inspired me to solve a big problem : know if an iframe is loaded.
My solution for download file with iframe, and fire an event with browser that does not support onload event is to give a multipart response.
the first part is the file, and the second is anything you want, either a &#039;special&#039; string or an html page that contains a .
In my case, I just try if the Iframe contains the string &#039;download_done&#039; every 100ms... Another dirty hack ;)

my simple class :

#-*- coding: utf-8 -*-

from hashlib import sha256
import random
import time

CRLF = &#039;\r\n&#039;

class MultipartResponse(object):

    def __init__(self, multipart=&#039;mixed&#039;, boundary=None):
        if boundary is None:
            random.seed(time.time())
            rand = random.randint(1000,999999999)
            self.boundary = sha256(str(rand)).hexdigest()
        self.content_type = &#039;multipart/&#039;+multipart+&#039;; boundary=%s&#039; % self.boundary
        self.parts = []


    def _addPart(self, data, headers):
        part = [&#039;--&#039; + self.boundary] + headers + [&#039;&#039;] + [data]
        self.parts.append(CRLF.join(part))

    def addText(self, text, charset=&quot;utf-8&quot;):
        content_type = &quot;Content-Type: text/plain; charset=%s&quot; % charset
        self._addPart(text, [content_type])

    def addHTML(self, html, charset=&quot;utf-8&quot;):
        content_type = &quot;Content-Type: text/html; charset=%s&quot; % charset
        self._addPart(html, [content_type])

    def addFile(self, data, mime_type, filename, charset=&quot;utf-8&quot;):
        content_type = &quot;Content-Type: %s; charset=%s&quot; % (mime_type, charset)
        content_disposition = &quot;Content-disposition: attachment; filename=%s&quot; % filename
        self._addPart(data, [content_type, content_disposition])

    def render(self):
        all = self.parts + [&#039;--&#039; + self.boundary + &#039;--&#039;] + [&#039;&#039;]
        return CRLF.join(all)

You can use it easely :

multipart = Multipart()
multipart.addFile(&quot;pif;paf;pouf&quot;, &#039;text/csv&#039;, &#039;export.csv&#039;) # your data here !
multipart.addText(&#039;download_done&#039;)
response.setHeader(&#039;Content-Type&#039;, multipart.content_type)
return multipart.render()</description>
		<content:encoded><![CDATA[<p>Hi, your post inspired me to solve a big problem : know if an iframe is loaded.<br />
My solution for download file with iframe, and fire an event with browser that does not support onload event is to give a multipart response.<br />
the first part is the file, and the second is anything you want, either a &#8216;special&#8217; string or an html page that contains a .<br />
In my case, I just try if the Iframe contains the string &#8216;download_done&#8217; every 100ms&#8230; Another dirty hack <img src='http://stacyprowell.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>my simple class :</p>
<p>#-*- coding: utf-8 -*-</p>
<p>from hashlib import sha256<br />
import random<br />
import time</p>
<p>CRLF = &#8216;\r\n&#8217;</p>
<p>class MultipartResponse(object):</p>
<p>    def __init__(self, multipart=&#8217;mixed&#8217;, boundary=None):<br />
        if boundary is None:<br />
            random.seed(time.time())<br />
            rand = random.randint(1000,999999999)<br />
            self.boundary = sha256(str(rand)).hexdigest()<br />
        self.content_type = &#8216;multipart/&#8217;+multipart+&#8217;; boundary=%s&#8217; % self.boundary<br />
        self.parts = []</p>
<p>    def _addPart(self, data, headers):<br />
        part = ['--' + self.boundary] + headers + [''] + [data]<br />
        self.parts.append(CRLF.join(part))</p>
<p>    def addText(self, text, charset=&#8221;utf-8&#8243;):<br />
        content_type = &#8220;Content-Type: text/plain; charset=%s&#8221; % charset<br />
        self._addPart(text, [content_type])</p>
<p>    def addHTML(self, html, charset=&#8221;utf-8&#8243;):<br />
        content_type = &#8220;Content-Type: text/html; charset=%s&#8221; % charset<br />
        self._addPart(html, [content_type])</p>
<p>    def addFile(self, data, mime_type, filename, charset=&#8221;utf-8&#8243;):<br />
        content_type = &#8220;Content-Type: %s; charset=%s&#8221; % (mime_type, charset)<br />
        content_disposition = &#8220;Content-disposition: attachment; filename=%s&#8221; % filename<br />
        self._addPart(data, [content_type, content_disposition])</p>
<p>    def render(self):<br />
        all = self.parts + ['--' + self.boundary + '--'] + ['']<br />
        return CRLF.join(all)</p>
<p>You can use it easely :</p>
<p>multipart = Multipart()<br />
multipart.addFile(&#8220;pif;paf;pouf&#8221;, &#8216;text/csv&#8217;, &#8216;export.csv&#8217;) # your data here !<br />
multipart.addText(&#8216;download_done&#8217;)<br />
response.setHeader(&#8216;Content-Type&#8217;, multipart.content_type)<br />
return multipart.render()</p>
]]></content:encoded>
	</item>
</channel>
</rss>

