开发者

with curl, how do I post a form with a file and stream that file?

I need to upload a file through a form. The form has other fields along with the file.

Right now I have:

class FileReader:
    def __init__(self, fp):
        self.fp = fp
    def read_callback(self, size):
        return self.fp.read(size)

c = pycurl.Curl()
c.setopt(pycurl.URL, url)
c.se开发者_开发技巧topt(pycurl.UPLOAD, 1)
c.setopt(pycurl.READFUNCTION, FileReader(open(filename, 'rb')).read_callback)
filesize = os.path.getsize(filename)
c.setopt(pycurl.INFILESIZE, filesize)

I'm wondering how would I include other form fields and stream the file together? I'm not sure how since i need to use pycurl.UPLOAD 1.

Also how would I include the form field name for the file?


Here's an example based on the source code of a pycurl test that may help:

import pycurl

fields = [('field1', 'this is a test using httppost & stuff'),
    ('field2', (pycurl.FORM_FILE, 'file1.txt', pycurl.FORM_FILE, 'file2.txt')),
    ('field3', (pycurl.FORM_CONTENTS, 'this is wei\000rd, but null-bytes are okay'))
]

c = pycurl.Curl()
c.setopt(c.URL, 'http://www.example.com')
c.setopt(c.HTTPPOST, fields)
c.perform()
c.close()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜