I'm using cherrypy to save a WAV file from the browser to local disk. Incoming:
{'audio_file': [<cherrypy._cpreqbody.Part object at 0x7fd95a409a90>, <cherrypy._cpreqbody.Part object at 0x7fd95a178190>], 'user_data': u'{"id":"1255733204",'audio_length': [u'10.03102', u'22.012517', u'22.012517']}
I get this error:
try:
f = open('/audiostory/'+filename,'wb')
logging.debug( '[SAVEAUDIO] tried to write all of audio_file input at once' )
f.write(kw.get('audio_file'))
f.close()
logging.debug( ('saved media file %s to /audiostory/' % f.name) )
except Exception as e:
logging.debug( ('saved media NOT saved %s because %s' % (f.name,str(e))) )
"Must be convertible to a buffer, not Part."
So how do I handle this kind of data, that cherrypy is converting to a '.Part' but it should be raw wav data? Am I missing a header or something?
UPDATED
Jason - you'll see I purposely did not send any header or whatnot because I wanted to see what cherrypy would deliver raw. Here is the cherrypy site.py file:
@cherrypy.expose
def saveaudio(self, *args, **kw):
import audiosave
return audiosave.raw_audio(kw.get('audio_file'))
and the function within audiosave:
def raw_audio(raw):
""" is fed a raw stream of data (I think) and saves it to disk, with logging. """
import json
import logging
LOG_FILENAME = 'error.log'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)
logging.debug( '[SAVEAUDIO(raw)]'+str(raw) )
filename = assign_filename()
try:
#SAVE FILE TO DISK /s/audiostory/
f = open('/home/djotjog/webapps/s/audiostory/'+filename,'wb')
logging.debug( '[SAVEAUDIO] tried to write all of audio_file input at once' )
f.write(raw)
f.close()
logging.debug( ('media SAVED %s' % f.name) )
except Exception as e:
logging.debug( ('media NOT saved %s because %s' % (f.name,str(e))) )
return json.dumps({"result":"414","message":"ERROR: Error saving Media file "+f.name})
return raw
I also tried f.write(raw.file.read()) but same error occurs.