By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,035 Members | 1,384 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,035 IT Pros & Developers. It's quick & easy.

multi-threaded webcam with SimpleAsyncHTTPServer.py

P: n/a
The class seems to work pretty well - very basic and fast, it just
serves images from the specified port and camera device (USB cam).
I added an ImageServer class and some code to __main__

class ImageServer(RequestHandler):
def __init__(self, conn, addr, server):
asynchat.async_chat.__init__(self,conn)
self.client_address = addr
self.connection = conn
self.server = server
self.set_terminator ('\r\n\r\n')
self.incoming = deque()
self.outgoing = deque()
self.rfile = None
self.wfile = writewrapper(self.outgoing,
-self.use_buffer or self.blocksize)
self.found_terminator = self.handle_request_line
self.request_version = "HTTP/1.1"
self.code = None

def send_head(self):
buff, width, height = cam.dev.getbuffer()
imPIL = (Image.frombuffer("RGB", (width, height), buff,
"raw", "BGR", 0, -1) )
imPIL.save(PILFile, "JPEG")
self.send_response(200)
self.send_header("Content-type", "image/jpeg")
self.send_header("Content-Length: ", str(PILFile.len))
self.end_headers()

self.wfile.write(PILFile.getvalue())
return
in __main__
....
parser.add_option('-3', dest='server',
help='Run the server for only cam images',
action='store_const', const=3)
....
if options.server==3:
from StringIO import StringIO
from PIL import Image
if sys.platform == 'win32':
import VideoCapture
try:
del(cam)
gc.collect()
print "deleted old cam instance"
except: pass
cam = VideoCapture.Device(devnum=options.devNum,
showVideoWindow=0)
print cam
buff, width, height = cam.dev.getbuffer()
PILFile = StringIO()
else:
pass
else:
if options.root is None:
parser.error("Need root path to start server")
if not os.path.isdir(options.root):
parser.error("Root path does not exist")
os.chdir(options.root)

I run it with:
>python SimpleAsyncHTTPServer.py -p8888 -3
I'll be trying implementing some streaming next.
Question, though: how can I unblock asyncore.loop(), or at least be
able to interrupt it? To kill this server I need to hit CNTRL-C and
then attempt to GET an image from Firefox, Python then throws
KetboardInterrupt.

Note that I haven't tried it on *NIX so the sys.platform bit needs
more implementing as well.

Other suggestions?

Ray
Jan 8 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Ray Schumacher wrote:
I'll be trying implementing some streaming next.
Question, though: how can I unblock asyncore.loop(),
Not at all. That's the way event loops work.
or at least be able to interrupt it?
Sorry for the stupid question, but why would you want to do that?
Other suggestions?
Yes, why do you call it multithreaded when it's multiplexing? (or
isn't it?)

Regards,
Björn

--
BOFH excuse #10:

hardware stress fractures

Jan 8 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.