473,387 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

multipart/form-data in an HTTP client

I'm writing some code to upload photos to Flickr. The Photo Upload API
requires documents be POSTed via a multipart/form-data request. I was
surprised to learn that Python 2.3's HTTP clients don't support this
form of POSTs. There is support in cgi.py, for servers.

There are some implementations of multipart/form-data on ASPN:
http://aspn.activestate.com/ASPN/Coo.../Recipe/146306
urllib2_file seems to meet my needs, but I'm not wild about how it's
implemented. Is there some other recommended way to do
multipart/form-data uploads with HTTP in Python?
References:
http://www.flickr.com/services/api/
http://aspn.activestate.com/ASPN/Coo.../Recipe/146306
http://www.faqs.org/rfcs/rfc1867.html
Jul 18 '05 #1
5 5334
On Wed, 01 Sep 2004 14:52:33 GMT, Nelson Minar <ne****@monkey.org> wrote:
There are some implementations of multipart/form-data on ASPN:
http://aspn.activestate.com/ASPN/Coo.../Recipe/146306
urllib2_file seems to meet my needs, but I'm not wild about how it's
implemented. Is there some other recommended way to do
multipart/form-data uploads with HTTP in Python?


I like ClientForm. I hope one day it or something like it will be in the stdlib.
Jul 18 '05 #2
Nelson Minar <ne****@monkey.org> writes:
I'm writing some code to upload photos to Flickr. The Photo Upload API
requires documents be POSTed via a multipart/form-data request. I was
surprised to learn that Python 2.3's HTTP clients don't support this
form of POSTs. There is support in cgi.py, for servers.
"Not supported" is an exaggeration, perhaps: there isn't special
support to make it especially easy, true, but neither is there
anything about urllib2 that makes it harder than it should be given
the level of the interface exposed: you just have to add the right
HTTP headers and HTTP request body data.

There are some implementations of multipart/form-data on ASPN:
http://aspn.activestate.com/ASPN/Coo.../Recipe/146306
urllib2_file seems to meet my needs, but I'm not wild about how it's
implemented.


What's wrong with that implementation? Looks reasonable to me, though
it seems to have a hack to work around bad servers that is different to
the one I have in my own code. I can well believe that both hacks are
required to work with as many servers as possible :-(
John
Jul 18 '05 #3
Nelson Minar wrote:
I'm writing some code to upload photos to Flickr. The Photo Upload API
requires documents be POSTed via a multipart/form-data request. I was
surprised to learn that Python 2.3's HTTP clients don't support this
form of POSTs. There is support in cgi.py, for servers.

There are some implementations of multipart/form-data on ASPN:
http://aspn.activestate.com/ASPN/Coo.../Recipe/146306
urllib2_file seems to meet my needs, but I'm not wild about how it's
implemented. Is there some other recommended way to do
multipart/form-data uploads with HTTP in Python?


I've been using something closely modelled on that Cookbook recipe,
without any real problems. (I've updated it to use HTTP[S]Connection()
and return the response object, and in my case I'm connecting to an
HTTPS server, but these are trivial modifications.)

I, too, was surprised that the existing Python libraries don't directly
support multipart/form-data already, and I hope that this gets added in
soon.

Jeff Shannon
Technician/Programmer
Credit International

Jul 18 '05 #4
Jeff Shannon <je**@ccvcorp.com> writes:
[...]
I've been using something closely modelled on that Cookbook recipe,
without any real problems. (I've updated it to use
HTTP[S]Connection() and return the response object, and in my case I'm
connecting to an HTTPS server, but these are trivial modifications.)

I, too, was surprised that the existing Python libraries don't
directly support multipart/form-data already, and I hope that this
gets added in soon.

[...]

Have you published your function? If not, please do.

Several people would like to see such a function in 2.5 (including
me), and your function sounds a good candidate. Maybe a little
polishing is required, but don't let that stop you: if you don't get
time to do that polishing, this is a rare occasion when somebody else
is likely to push it the small additional distance it would need to go
in order to get into the Python stdlib!
John
Jul 18 '05 #5
John J. Lee wrote:
Jeff Shannon <je**@ccvcorp.com> writes:
[...]

I've been using something closely modelled on that Cookbook recipe,
without any real problems. (I've updated it to use
HTTP[S]Connection() and return the response object, and in my case I'm
connecting to an HTTPS server, but these are trivial modifications.)

[...]

Have you published your function? If not, please do.


Here it is, for what it's worth. (Hopefully my mailer won't mangle the
indentation...) As I noted, the differences between this and the
Cookbook recipe are minimal -- I literally copied the recipe into my
editor and made a few changes.

If I were going to put further effort into polishing it for library use
(which I probably won't have much opportunity to do), I'd make at least
two changes. One would be to set it up to select between HTTP and
HTTPS. (It currently tries to determine the protocol from the hostname
string, but does nothing with that information.) The other would be to
enable the file-subpart to add a Content-type header. (This is
irrelevant for my particular application, as the form(s) I'm uploading
to don't care.)

In case it matters to anyone, inasmuch as this code is nearly identical
to the published recipe, my changes can be considered to be under the
same license as the original recipe. If someone feels like using this
in any way, feel free.

Jeff Shannon
Technician/Programmer
Credit International
#! /usr/bin/python

import httplib

def post_multipart(host, selector, fields, files):
"""
Post fields and files to an http host as multipart/form-data.
fields is a sequence of (name, value) elements for regular form fields.
files is a sequence of (name, filename, value) elements for data to be uploaded as files.
Return an appropriate httplib.HTTPResponse object.
"""
content_type, body = encode_multipart_formdata(fields, files)
protocol = host.split(':')[0]
h = httplib.HTTPSConnection(host)
h.putrequest('POST', selector)
h.putheader('content-type', content_type)
h.putheader('content-length', str(len(body)))
h.endheaders()
h.send(body)
response = h.getresponse()
return response

def encode_multipart_formdata(fields, files):
"""
fields is a sequence of (name, value) elements for regular form fields.
files is a sequence of (name, filename, value) elements for data to be uploaded as files.
Return (content_type, body) ready for httplib.HTTPConnection instance
"""
BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_---$---'
CRLF = '\r\n'
L = []
for (key, value) in fields:
L.append('--' + BOUNDARY)
L.append('Content-Disposition: form-data; name="%s"' % key)
L.append('')
L.append(value)
for (key, filename, value) in files:
L.append('--' + BOUNDARY)
L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
L.append('')
L.append(value)
L.append('--' + BOUNDARY + '--')
L.append('')
body = CRLF.join(L)
content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
return content_type, body

Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: Niels Berkers | last post by:
Hi, i'd like to host my web pages using multiparts to reduce the number of hits on the server. i know this isn't a real PHP subject, but i'll try it anyway. i've been searching the web for...
6
by: LRW | last post by:
Because I've had recipients of our newsletter tell us they can't (or won't) receive HTML e-mail, I found out how to make an e-mail that sends both HTML and a plaintext version in a multipart...
2
by: Damien | last post by:
Hi to all, After hours of attempts and "googling", I'm still pulling my hair off my head when I try to send multipart html emails. It "works" on PCs with Outlook when I juste send a single...
4
by: Hunter Peress | last post by:
I have been unable to write a script that can do (and receieve) a multipart form upload. Also, it seems that there really are differences between python's implementation and else's. Can someone...
4
by: John Fereira | last post by:
So, one of the limitations of multipart-form handling is that when an <input type="file" ..> tag is used it will bring up a window which allows a user to select a file for upload but won't allow...
3
by: Li Zhang | last post by:
I know I can use controlName.Value to retrieve the form fields value if I am in that page. But if I am out of the page, for example I am in a HttpModule, I want to retrieve a hidden filed value, Is...
1
by: Juan Dent | last post by:
Hi, I am naming all my assemblies in a multipart format, e.g.: Company.Project.Component.dll This has worked perfectly with C# assemblies. The problem arises with C++ native COM servers...
2
by: madmak | last post by:
Hi, I am a noob with PHP and need some asistance regarding PHP and lotus notes. I am trying to create a multipart message in PHP to send mail via lotus notes. Here is the code snippet. ...
6
by: fnoppie | last post by:
Hi, I am near to desperation as I have a million things to get a solution for my problem. I have to post a multipart message to a url that consists of a xml file and an binary file (pdf)....
1
by: Gabriel Genellina | last post by:
En Fri, 29 Aug 2008 14:41:53 -0300, Ron Brennan <brennan.ron@gmail.com> escribi�: What is a "multipart"? I know MIME multipart messages but they don't seem to apply here... From your other...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.