user4719989 user4719989 - 5 months ago 71
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 =, 'rb')


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.

import socket

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)

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