2

I am tring to post an HTTP request using httplib2 that contains some xml and some binary data using this infoset:

MIME-Version: 1.0
Content-Type: Multipart/Related;boundary=MIME_boundary;
...
--MIME_boundary
Content-Type: application/xop+xml; 

// [the xml string goes here...]

--MIME_boundary
Content-Type: image/png
Content-Transfer-Encoding: binary
Content-ID: <http://example.org/me.png>

// [the binary octets for png goes here...]

My approach is to generate a txt file, and then fill in the xml and the binary data.

I am having problem writing binary data to the file reading from the png with this:

pngfile = open(pngfile, "rb")
bindata = pngfile.read()

What's the best way to do this?

schlamar
  • 9,238
  • 3
  • 38
  • 76
Carlo Piva
  • 21
  • 3

1 Answers1

0

My advice is to use Python's standard mime library as in these examples. Try with this:

from email.mime.image import MIMEImage
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart

msg = MIMEMultipart()
# Attach XML
xml = MIMEBase('application','xop+xml')
xml.set_payload(<xml data here>)
msg.attach(xml)

# Attach image
img = MIMEImage(<image data here>, _subtype='png')
msg.attach(img)

# Export the infoset
print msg.as_string()
acondolu
  • 333
  • 1
  • 9