我需要通过表单上传文件。表单还包含其他字段和文件。
现在我有:
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.setopt(pycurl.UPLOAD, 1)
c.setopt(pycurl.READFUNCTION, FileReader(open(filename, 'rb')).read_callback)
filesize = os.path.getsize(filename)
c.setopt(pycurl.INFILESIZE, filesize)
我想知道如何包含其他表单字段并将文件串流在一起?我不确定自从我需要使用pycurl.UPLOAD 1。
另外,我如何包含文件的表单字段名称?
答案 0 :(得分:2)
以下是基于source code of a pycurl
test的示例,可能有所帮助:
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()