curl-and-python
Re: how to POST file upload with pycurl
Date: Sat, 03 Mar 2007 01:31:07 -0500
When I want to do this I use this little class to build the list of nested
tuple that you need.
class CurlForm(list):
def add_string(self, name, str, type=None):
options = [pycurl.FORM_CONTENTS, str]
self.__add_optional(options, pycurl.FORM_CONTENTTYPE, type)
self += (name, tuple(options)),
def add_file(self, name, file, type=None, filename=None):
assert file is not __builtins__.file
options = [pycurl.FORM_FILE, file]
self.__add_optional(options, pycurl.FORM_CONTENTTYPE, type)
self.__add_optional(options, pycurl.FORM_FILENAME, filename)
self += (name, tuple(options)),
@staticmethod
def __add_optional(list, flag, val):
if val is not None:
list += flag, val
The code in pycurl that handles this option is at
http://pycurl.cvs.sourceforge.net/pycurl/pycurl/src/pycurl.c?revision=1.116&view=markup&pathrev=REL_7_15_5_1#l_1670
which is easy to follow.
Nathan Moore
--On Friday, March 02, 2007 3:31 PM +0100 Kjetil Jacobsen
<kjetilja_at_gmail.com> wrote:
> hi,
>
> look at the pycurl tests in the distribution as they might help, for
> instance this one:
>
> http://pycurl.cvs.sourceforge.net/*checkout*/pycurl/pycurl/tests/test_pos
> t2.py?revision=1.13&content-type=text%2Fplain
>
> regards,
> - kjetil
>
> On 3/2/07, PoisoneR <poisonertmp_at_gmail.com> wrote:
>> Hi,
>> I am trying to submit file via HTTP POST.
>> But I can't find any examples in the net.
>> I tried somethin like this
>>
>> def sendupload():
>> c = pycurl.Curl()
>> c.setopt(c.USERAGENT, ua)
>> url = 'http://localhost/post.php'
>> c.setopt(c.POST, 1)
>> c.setopt(c.URL, url)
>> f = open('clip.3gp', 'rb')
>> c.setopt(c.FORM_FILE, f)
>> fc = 'cookie.txt'
>> c.setopt(c.COOKIEFILE, fc)
>> c.setopt(c.COOKIEJAR, fc)
>> f = open('sendupload.html', 'wb')
>> c.setopt(c.WRITEDATA, f)
>> c.setopt(c.VERBOSE, 1)
>> c.perform()
>> c.close()
>>
>> but it doesn't work
>>
>> c.setopt(c.FORM_FILE, f)
>> TypeError: files are not supported for this option
>>
>> i tried and with filename string instead file object, but is the same.
>> What variable type this wants?
>> _______________________________________________
>> http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-python
>>
> _______________________________________________
> http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-python
>
_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-python
Received on 2007-03-03