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()
--------------------------------------------------
只提交文件
def upload_wav( wavfile, url=None, **kwargs ):
"""Upload a wav file to the server, return the response."""
class responseCallback:
"""Store the server response."""
def __init__(self):
self.contents=''
def body_callback(self, buf):
self.contents = self.contents + buf
def decode( self ):
self.contents = urllib.unquote(self.contents)
try:
self.contents = simplejson.loads(self.contents)
except:
return self.contents
t = responseCallback()
c = pycurl.Curl()
c.setopt(c.POST,1)
c.setopt(c.WRITEFUNCTION, t.body_callback)
c.setopt(c.URL,url)
postdict = [
('userfile',(c.FORM_FILE,open(wavfile, 'r').read())), #wav file to post
]
#If there are extra keyword args add them to the postdict
for key in kwargs:
postdict.append( (key,kwargs[key]) )
c.setopt(c.HTTPPOST,postdict)
c.setopt(c.VERBOSE,verbose)
c.perform()
c.close()
t.decode()
return t.contents