<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>stacyprowell.com &#187; multipart/form-data</title>
	<atom:link href="http://stacyprowell.com/blog/tag/multipartform-data/feed/" rel="self" type="application/rss+xml" />
	<link>http://stacyprowell.com/blog</link>
	<description>Ugh, Stacy's talking again...</description>
	<lastBuildDate>Tue, 27 Apr 2010 20:09:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Handling multipart/form-data in Python</title>
		<link>http://stacyprowell.com/blog/2009/05/29/handling-multipartform-data-in-python/</link>
		<comments>http://stacyprowell.com/blog/2009/05/29/handling-multipartform-data-in-python/#comments</comments>
		<pubDate>Fri, 29 May 2009 14:43:35 +0000</pubDate>
		<dc:creator>stacy</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[multipart]]></category>
		<category><![CDATA[multipart/form-data]]></category>

		<guid isPermaLink="false">http://stacyprowell.com/blog/?p=225</guid>
		<description><![CDATA[I need to generate multipart/form-data (see here) messages from Python.  Never mind why.  I dug around in the documentation for httplib, urllib, and urllib2, but it seems this is not currently supported (it&#8217;s Issue 3244).  I didn&#8217;t like the code I found on the web to do it, because I needed to [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_235" class="wp-caption alignright" style="width: 160px"><img class="size-full wp-image-235" title="programming" src="http://stacyprowell.com/blog/wp-content/uploads/2009/05/1ebeb1d2f212046a4e47fcd414dbad9b1-150x150.jpg" alt="Programming" width="150" height="150" /><p class="wp-caption-text">Programming</p></div>
<p>I need to generate <tt>multipart/form-data</tt> (see <a href="http://www.w3.org/TR/html401/interact/forms.html">here</a>) messages from Python.  Never mind why.  I dug around in the documentation for httplib, urllib, and urllib2, but it seems this is not currently supported (it&#8217;s <a href="http://bugs.python.org/issue3244">Issue 3244</a>).  I didn&#8217;t like the code I found on the web to do it, because I needed to set additional headers on each piece.  So&#8230; I wrote something.  Here it is.  If it&#8217;s useful to you, great!  If you find bugs in it, please let me know.  I think this is pretty easy to use.<br />
<span id="more-225"></span><br />
Make an instance of Multipart, and then add parts using the field and file methods.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt;&gt;&gt; from multipart import Multipart
&gt;&gt;&gt; m = Multipart()
&gt;&gt;&gt; m.field('search','searchish term')
&gt;&gt;&gt; m.file('greet','greet.txt','Hello multipart world!',{'Content-Type':'text/text'})
&gt;&gt;&gt; ct,body = m.get()
&gt;&gt;&gt; print ct
multipart/form-data; boundary=----------AaB03x
&gt;&gt;&gt; print body
------------AaB03x
Content-Type: application/octet-stream
Content-Disposition: form-data; name=&quot;search&quot;
&nbsp;
searchish term
------------AaB03x
Content-Type: text/text
Content-Disposition: form-data; name=&quot;greet&quot;; filename=&quot;greet.txt&quot;
&nbsp;
Hello multipart world!
------------AaB03x--</pre></div></div>

<p>If no content type is specified for a value, then the default of application/octet-stream is chosen.  If none is specified for a file, then the mime libraries are consulted to guess one based on the filename, and again the default is application/octet-stream if none can be guessed.</p>
<p>If you want to specify the content type, make sure to use the string &#8216;Content-Type&#8217; (note the caps) or it will be ignored.  I&#8217;ve seen the capitalization all over the map, but needed to choose one to use in the case-sensitive dict.  If you don&#8217;t like my choice&#8230; change the code.  See the constants at the start of the Client class.</p>
<p>Here&#8217;s how you might continue the above example to send this out in an HTTP request.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">request = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">Request</span><span style="color: black;">&#40;</span>url=<span style="color: #483d8b;">'http://my.fake.server'</span>,
    headers=<span style="color: black;">&#123;</span><span style="color: #483d8b;">'Content-Type'</span>:ct<span style="color: black;">&#125;</span>,
    data=body<span style="color: black;">&#41;</span>
reply = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> reply.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Finally, here&#8217;s the code.  Enjoy!  This comes, of course, with <em>no warranty</em>, use at your own risk, etc.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/python</span>
<span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
Classes for using multipart form data from Python, which does not (at the
time of writing) support this directly.
&nbsp;
To use this, make an instance of Multipart and add parts to it via the factory
methods field and file.  When you are done, get the content via the get method.
&nbsp;
@author: Stacy Prowell (http://stacyprowell.com)
'</span><span style="color: #483d8b;">''</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">mimetypes</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Part<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
    Class holding a single part of the form.  You should never need to use
    this class directly; instead, use the factory methods in Multipart:
    field and file.
    '</span><span style="color: #483d8b;">''</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># The boundary to use.  This is shamelessly taken from the standard.</span>
    BOUNDARY = <span style="color: #483d8b;">'----------AaB03x'</span>
    CRLF = <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>'</span>
    <span style="color: #808080; font-style: italic;"># Common headers.</span>
    CONTENT_TYPE = <span style="color: #483d8b;">'Content-Type'</span>
    CONTENT_DISPOSITION = <span style="color: #483d8b;">'Content-Disposition'</span>
    <span style="color: #808080; font-style: italic;"># The default content type for parts.</span>
    DEFAULT_CONTENT_TYPE = <span style="color: #483d8b;">'application/octet-stream'</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, name, filename, body, headers<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
        Make a new part.  The part will have the given headers added initially.
&nbsp;
        @param name: The part name.
        @type name: str
        @param filename: If this is a file, the name of the file.  Otherwise
                        None.
        @type filename: str
        @param body: The body of the part.
        @type body: str
        @param headers: Additional headers, or overrides, for this part.
                        You can override Content-Type here.
        @type headers: dict
        '</span><span style="color: #483d8b;">''</span>
        <span style="color: #008000;">self</span>._headers = headers.<span style="color: #dc143c;">copy</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>._name = name
        <span style="color: #008000;">self</span>._filename = filename
        <span style="color: #008000;">self</span>._body = body
        <span style="color: #808080; font-style: italic;"># We respect any content type passed in, but otherwise set it here.</span>
        <span style="color: #808080; font-style: italic;"># We set the content disposition now, overwriting any prior value.</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>._filename == <span style="color: #008000;">None</span>:
            <span style="color: #008000;">self</span>._headers<span style="color: black;">&#91;</span>Part.<span style="color: black;">CONTENT_DISPOSITION</span><span style="color: black;">&#93;</span> = \
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">'form-data; name=&quot;%s&quot;'</span> <span style="color: #66cc66;">%</span> <span style="color: #008000;">self</span>._name<span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>._headers.<span style="color: black;">setdefault</span><span style="color: black;">&#40;</span>Part.<span style="color: black;">CONTENT_TYPE</span>,
                                     Part.<span style="color: black;">DEFAULT_CONTENT_TYPE</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #008000;">self</span>._headers<span style="color: black;">&#91;</span>Part.<span style="color: black;">CONTENT_DISPOSITION</span><span style="color: black;">&#93;</span> = \
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">'form-data; name=&quot;%s&quot;; filename=&quot;%s&quot;'</span> <span style="color: #66cc66;">%</span>
                 <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._name, <span style="color: #008000;">self</span>._filename<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>._headers.<span style="color: black;">setdefault</span><span style="color: black;">&#40;</span>Part.<span style="color: black;">CONTENT_TYPE</span>,
                                     <span style="color: #dc143c;">mimetypes</span>.<span style="color: black;">guess_type</span><span style="color: black;">&#40;</span>filename<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
                                     <span style="color: #ff7700;font-weight:bold;">or</span> Part.<span style="color: black;">DEFAULT_CONTENT_TYPE</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> get<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
        Convert the part into a list of lines for output.  This includes
        the boundary lines, part header lines, and the part itself.  A
        blank line is included between the header and the body.
&nbsp;
        @return: Lines of this part.
        @rtype: list
        '</span><span style="color: #483d8b;">''</span>
        lines = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
        lines.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'--'</span> + Part.<span style="color: black;">BOUNDARY</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> <span style="color: black;">&#40;</span>key, val<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>._headers.<span style="color: black;">items</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
            lines.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'%s: %s'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>key, val<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        lines.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>
        lines.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._body<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> lines
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Multipart<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
    Encapsulate multipart form data.  To use this, make an instance and then
    add parts to it via the two methods (field and file).  When done, you can
    get the result via the get method.
&nbsp;
    See http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2 for
    details on multipart/form-data.
&nbsp;
    Watch http://bugs.python.org/issue3244 to see if this is fixed in the
    Python libraries.
&nbsp;
    @return: content type, body
    @rtype: tuple
    '</span><span style="color: #483d8b;">''</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">parts</span> = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> field<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, name, value, headers=<span style="color: black;">&#123;</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
        Create and append a field part.  This kind of part has a field name
        and value.
&nbsp;
        @param name: The field name.
        @type name: str
        @param value: The field value.
        @type value: str
        @param headers: Headers to set in addition to disposition.
        @type headers: dict
        '</span><span style="color: #483d8b;">''</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">parts</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span>Part<span style="color: black;">&#40;</span>name, <span style="color: #008000;">None</span>, value, headers<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #008000;">file</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, name, filename, value, headers=<span style="color: black;">&#123;</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
        Create and append a file part.  THis kind of part has a field name,
        a filename, and a value.
&nbsp;
        @param name: The field name.
        @type name: str
        @param value: The field value.
        @type value: str
        @param headers: Headers to set in addition to disposition.
        @type headers: dict
        '</span><span style="color: #483d8b;">''</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">parts</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span>Part<span style="color: black;">&#40;</span>name, filename, value, headers<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> get<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
        Get the multipart form data.  This returns the content type, which
        specifies the boundary marker, and also returns the body containing
        all parts and bondary markers.
&nbsp;
        @return: content type, body
        @rtype: tuple
        '</span><span style="color: #483d8b;">''</span>
        <span style="color: #008000;">all</span> = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> part <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">parts</span>:
            <span style="color: #008000;">all</span> += part.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">all</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'--'</span> + Part.<span style="color: black;">BOUNDARY</span> + <span style="color: #483d8b;">'--'</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">all</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>
        <span style="color: #808080; font-style: italic;"># We have to return the content type, since it specifies the boundary.</span>
        content_type = <span style="color: #483d8b;">'multipart/form-data; boundary=%s'</span> <span style="color: #66cc66;">%</span> Part.<span style="color: black;">BOUNDARY</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> content_type, Part.<span style="color: black;">CRLF</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #008000;">all</span><span style="color: black;">&#41;</span></pre></div></div>

<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fstacyprowell.com%2Fblog%2F2009%2F05%2F29%2Fhandling-multipartform-data-in-python%2F&amp;linkname=Handling%20multipart%2Fform-data%20in%20Python"><img src="http://stacyprowell.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://stacyprowell.com/blog/2009/05/29/handling-multipartform-data-in-python/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
