Skip to content

Instantly share code, notes, and snippets.

@lidio601
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lidio601/5c683ebf2c81c550628a to your computer and use it in GitHub Desktop.
Save lidio601/5c683ebf2c81c550628a to your computer and use it in GitHub Desktop.
Apache+mod_python+Python MJPEG output handler. Here I needed a MJPEG video output by a web URI. In this case I used the mod_python to handle a web request by the Apache webserver. The Python script was simply loading a list of images contained in a certain directory and that have been named in a numeric progressive way.
import httplib
import cgi
from mod_python import apache
import mod_python
import string
import os.path
import sys
import time
from time import gmtime
from time import strftime
"""
Handler function for the mod_python
"""
def handler(req):
fps = 3
form = mod_python.util.FieldStorage(req)
bound = "myboundary12312313"
req.content_type = 'multipart/x-mixed-replace; boundary=%s' % (bound)
req.media_type='image/jpeg'
i = 0
"""
Here I'm loading a list of images,
in a certain directory,
that have been named in a numeric progression
such as:
/var/data/images/image1.jpg
/var/data/images/image2.jpg
...
/var/data/images/image10.jpg
...
"""
while True:
f = open('/var/data/images/image'+str(i)+'.jpg')
image = f.read()
req.write("%s\r\nContent-Type: image/jpeg\r\nContent-Length: %s\r\n\r\n" % (bound,len(image)) )
req.write(image)
req.write("\r\n")
f.close()
i = (i + 1) % 3
time.sleep( 1 / ( float( fps ) ) )
return apache.OK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment