Serving a file through HTTP

Gerhard Häring gh at
Tue Mar 18 15:21:52 CET 2003

gumuz at wrote:
> Ah, I'm sorry, I left something unclear in my message I think.
> I mean, serving up binary files like music(mp3). It sounds very simple,
> but I have no clue how to stream such a file to the client. [...]

You're probably just thinking too complicated. From SimpleHTTPServer:

     def do_GET(self):
         """Serve a GET request."""
         f = self.send_head()
         if f:
             self.copyfile(f, self.wfile)

Let's adapt this slightly (untested):

     def do_GET(self):
         """Serve a GET request."""
         self.send_header("Content-Type", "audio/mp3")

         my_mp3_file = open("Metallica - Nothing Else Matters.mp3", "rb")

         # Reading the entire file at once is probably not that great a
         # strategy.


This would probably quite a boring MP3 server, always serving the same 
file ;-)

-- Gerhard

More information about the Python-list mailing list