I have a program that can generate video in real time. Now I would like to stream this video online while it is being generated. Does anybody know an easy way to do it? I tried the following method using CGI but somehow it didn't work.

I set the content-type to mpeg for example, and print out a chunk of data in the mpeg file periodically. But the video only lasts for very short amount of time and stop streaming. My code is something like this (in Python, but it should be very easy to read).

Code:
print "Content-type: video/mpeg"
print
f = open("test2.mpg")
while (True):
    st = f.read(1024*1024) # read in 1M of data in this file
    sys.stdout.write(st)
    time.sleep(0.5) # sleep for 0.5 second
Though this would work fine. I really don't see why the output of these two programs are different. But obviously I can't use this approach since i can't wait until the entire file is generated before reading in.

Code:
print "Content-type: video/mpeg"
print
f = open("test2.mpg")
print f.read()  #directly print out all the data in the file.