[Zope] File Upload from Python

Thomas B. Passin tpassin@mitretek.org
Thu, 28 Feb 2002 11:20:18 -0500


I don't think your message is constructed correctly.  I also don't see where
it incldues the file contents itself.  Rather than trying to construct the
MIME-encoded POST form yourself, use the MimeWriter and urllib standard
libraries.

The MimeWriter docs give an example of encoding multiple files into one MIME
document, and urllib has a command to send POST data in urlopen().  This are
what I would use (I have used urllib to POST data, but not file upload data
so far).

Cheers,

Tom P

[Zanotti Michele]

> Perhaps this is a Python question, not a Zope problem, but I hope someone
> can help me.
> I'm tryng to upload a file from PythonWin Ide (I have setted my Zope-test
> permissions to anonymous). I obtain a 400 error with this page:
"...Invalid
> request id omitted...": but I don't understand where are my error(s). This
> is my code:
> >>> import httplib
> >>> bd='--7d01ecf406a6'
> >>> body=bd+'\r\ncontent-disposition: form-data;
> name="id"\r\n\r\naloha\r\n'+bd+'\r\ncontent-disposition:   form-data;
> name="title"\r\n\nxx\r\n'+bd+'\r\ncontent-disposition: form-data;
> name="submit"\r\n\r\n add\r\n'
> >>> body=body+bd+'\r\ncontent-disposition: form-data; name="file";
> filename="prova.txt"\r\ncontent-type: text/plain\r\n\r\nprova\r\n'+bd+'--'
> >>> conn = httplib.HTTPConnection("mypc:8080")
> >>> conn.putrequest("POST", "/manage_addFile")
> >>> conn.putheader('content-type','multipart/form-data; boundary='+bd)
> >>> conn.putheader('content-length',str(lenb))
> >>> conn.endheaders()
> >>> conn.send(body)
> >>> r=conn.getresponse()
> >>> r.status
> 400
> >>> print r.read()
> ...<P><STRONG>Invalid request</STRONG></P>
>   The parameter, <em>id</em>, was omitted from the request.<p>...
> Can anyone help me? Thanks and excuse if my errors are so trivial...
> Michele
> P.S. perhaps a better solution for batch upload is Curl or pyCurl, but I
> find pycurl has too poor documentation for my level... Has anyone used it?