title: Send file to a browser using python socket programming
body: <p>I am trying to send a file(mp3) to a client(browser: Chrome) using python (only socket programming). My problem is that the file is getting downloaded instead of streaming on the browser. I am not able to understand where I am wrong. Please let me know.</p>
  
  <p>Also, the audio file that gets downloaded is not corrupted and I am able to play the audio.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>import socket
  import os
  import time
  from encodings.base64_codec import base64_encode
  serversocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  port=8000
  
  def createServer():
  
     serversocket.bind(("",port))
     serversocket.listen(5)
     file=open('music.mp3',"rb")
     z=file.read()
     var=len(z)
     file.close()
     (clientsocket, address) = serversocket.accept()
     clcommand=clientsocket.recv(1024).decode('utf8')
  
     header="HTTP/1.1 200 OK"
     header1="Content-Type: audio/mpeg"
     header2="Content-Length: {}".format(var)
     blank="\r\n"
  
     sendt=header+header1+header2+blank
     clientsocket.send(sendt.encode())
  
     file=open("music.mp3",'rb')
     z=file.read(512)
     while (z):
       try:
  
          clientsocket.send(z)
          z=file.read(512)
  
       except:
          print("done sending")
          exit()
  
  createServer()
  </code></pre>