How to parse multi-part content

Dave Kuhlman dkuhlman at rexx.com
Fri Sep 24 15:32:43 EDT 2004


Suppose that I have content that looks like what I've included at
the end of this message.  Is there something in the standard
Python library that will help me parse it, break into the parts
separated by the boundary strings, extract headers from each
sub-part, etc?

Do I need to add something like the following to the beginning?

    Content-Type: multipart/related;
        type="multipart/alternative";
        boundary="-----------------------------1646970154570313593966717980"

I've tried working with the email, mimetools, and multifile
modules in the standard library.  But my understanding of these
things is dim, and I have not had success.

Is there a beginner's guide somewhere that I should read?

In case you are curious, this is content posted to my Zope server
when I include an element '<input type="file" .../>' in my form.

Here is the content that I need to parse:


    -----------------------------1646970154570313593966717980
    Content-Disposition: form-data; name="xschemaContent"


    -----------------------------1646970154570313593966717980
    Content-Disposition: form-data; name="xschemaFile"; filename="po.xsd"
    Content-Type: application/octet-stream

    <xs:schema targetNamespace="http://openuri.org/easypo"
        xmlns:po="http://openuri.org/easypo"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        elementFormDefault="qualified">

        <xs:element name="purchase-order">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="customer" type="po:customer"/>
                    <xs:element name="date" type="xs:dateTime"/>
                    <xs:element name="line-item" type="po:line-item"
minOccurs="0" maxOccurs="unbounded"/>
                    <xs:element name="shipper" type="po:shipper"
minOccurs="0"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
        <xs:complexType name="customer">
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
                <xs:element name="address" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
        <xs:complexType name="line-item">
            <xs:sequence>
                <xs:element name="description" type="xs:string"/>
                <xs:element name="per-unit-ounces" type="xs:decimal"/>
                <xs:element name="price" type="xs:double"/>
                <xs:element name="quantity" type="xs:integer"/>
            </xs:sequence>
        </xs:complexType>
        <xs:complexType name="shipper">
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
                <xs:element name="per-ounce-rate" type="xs:decimal"/>
            </xs:sequence>
        </xs:complexType>
    </xs:schema>

    -----------------------------1646970154570313593966717980
    Content-Disposition: form-data; name="which"

    superclass
    -----------------------------1646970154570313593966717980
    Content-Disposition: form-data; name="Submit"

    Submit
    -----------------------------1646970154570313593966717980--



-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman



More information about the Python-list mailing list