user4719989 user4719989 - 17 days ago 14
Python Question

Send wav files through socket

I am trying to send a wav file through a socket.

I get the error:

TypeError: must be string or buffer, not instance





waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'rb')
my_socket.sendall(waveFile)

Answer

The wave doesn't give you general file I/O. It is used for getting media properties.

You could use just normal open/close, read/write.

sender.py:

import socket

(HOST,PORT)=('localhost',19123)
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM); s.connect((HOST,PORT))

with open('input', 'rb') as f:
  for l in f: s.sendall(l)
s.close()

receiver.py:

import socket

(HOST,PORT) = ('localhost',19123)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT)); s.listen(1); conn, addr = s.accept()

with open('output','wb') as f:
  while True:
    l = conn.recv(1024)
    if not l: break
    f.write(l)
s.close()