¿Cómo enviar un blob de audio de javascript a python?

Quiero enviar un blob de audio desde JS a la secuencia de comandos de python (que se ejecuta en el servidor). Mi JS ajax .. se ve algo como esto.

var fileType = 'audio'; var fileName = 'output.wav'; var formData = new FormData(); formData.append(fileType + '-filename', fileName); formData.append(fileType + '-blob', blob); $.ajax({ type: 'POST', url: 'http://localhost/python/audio.py', data: {audio:formData}, success: function(response) { alert(respose); } }); 

y mi script en python se parece a esto.

 #!/usr/bin/python3 print("Content-Type: text/html") print() import ssl import cgi import wave import contextlib form = cgi.FieldStorage() fname = form.getvalue("audio", "error") with contextlib.closing(wave.open(fname,'r')) as f: frames = f.getnframes() rate = f.getframerate() duration = frames / float(rate) print(duration) 

En este momento, solo estoy probando, así que debería darme la duración del archivo de audio. El blob se genera a través de record.js

Esto no funciona, ya que el python no puede identificar el archivo. ¿Alguna solución?

PS: Estoy usando el servidor Xampp, para ejecutarme en un host local.

En respuesta a Wojtek Marczenko: El error es

 [Mon Apr 04 18:26:09.537912 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215: Traceback (most recent call last):: /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html [Mon Apr 04 18:26:09.537978 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215: File "/home/shashank/project/dutchman/python/audio.py", line 10, in : /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html [Mon Apr 04 18:26:09.538002 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215: with contextlib.closing(wave.open(fname,'r')) as f:: /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html [Mon Apr 04 18:26:09.538024 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215: File "/usr/lib/python3.5/wave.py", line 499, in open: /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html [Mon Apr 04 18:26:09.538036 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215: return Wave_read(f): /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html [Mon Apr 04 18:26:09.538056 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215: File "/usr/lib/python3.5/wave.py", line 163, in __init__: /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html [Mon Apr 04 18:26:09.538065 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215: self.initfp(f): /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html [Mon Apr 04 18:26:09.538086 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215: File "/usr/lib/python3.5/wave.py", line 128, in initfp: /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html [Mon Apr 04 18:26:09.538097 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215: self._file = Chunk(file, bigendian = 0): /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html [Mon Apr 04 18:26:09.538110 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215: File "/usr/lib/python3.5/chunk.py", line 61, in __init__: /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html [Mon Apr 04 18:26:09.538119 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215: self.chunkname = file.read(4): /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html [Mon Apr 04 18:26:09.538132 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215: AttributeError: 'NoneType' object has no attribute 'read': /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html 

Parece que no estás enviando correctamente el blob como campo de formulario. La forma correcta de adjuntar blob a FormData sería formData.append(fileType, blob, fileName); . También debe adjuntar solo el formData en lugar de anidarlo en otro objeto:

 var formData = new FormData(); formData.append(fileType, blob, fileName); $.ajax({ type: 'POST', url: 'http://localhost/python/audio.py', data: formData, processData: false, // prevent jQuery from converting the data contentType: false, // prevent jQuery from overriding content type success: function(response) { alert(response); } }); 

Fuentes: https://developer.mozilla.org/en-US/docs/Web/API/FormData/append http://www.mattlunn.me.uk/blog/2012/05/sending-formdata-with-jquery -ajax /

En el lado de Python, necesitas usar el módulo CGI de acuerdo con los documentos de Python (no puedes publicar más enlaces). Creo que la forma correcta sería así:

 form = cgi.FieldStorage() fname = form["audio"].filename print "Got filename:", fname # in case of problems see if this looks ok with contextlib.closing(wave.open(fname,'r')) as f: ...