-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
72 lines (62 loc) · 2.5 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""TBW."""
import os
from pathlib import Path
import click
import tornado.ioloop
import tornado.web
import tornado.websocket
import camera_utils
import image_stream_handler
import download_handler
import tornado_utils
__version__ = "0.6"
@click.command()
@click.option('-p', '--port', default=0, help='IP port used for the web server (default: 0)')
@click.option('-a', '--address', default='0.0.0.0', help='IP address used for the web server (default: 0.0.0.0)')
@click.option('-v', '--verbosity', count=True, help='The verbosity level.')
@click.option('-s', '--simulate', is_flag=True, help='Enable simulated camera.')
@click.option('-m', '--mode', default='push', type=click.Choice(['get', 'push']), help='The mode of operation (default: push).')
@click.option('-v', '--verbosity', count=True, help='The verbosity level.')
@click.version_option(version=__version__)
def main(port=8888, address='0.0.0.0', simulate=False, mode='push', verbosity=0):
"""Tornado web server that streams webcam images over the network."""
tornado_utils.config_logging(verbosity)
pkg_dir = Path(__file__).absolute().parent
camera_class = [
camera_utils.WebCam,
camera_utils.SimCam
][simulate]
streamer_class = {
'push': image_stream_handler.ImagePushStreamHandler,
'get': image_stream_handler.ImageStreamHandler,
}[mode]
app = tornado.web.Application(
handlers=[
(r"/imagestream", streamer_class),
(r"/photo", download_handler.PhotoHandler),
(r"/download", download_handler.DownloadHandler),
(r"/", image_stream_handler.IndexPageHandler, {
"path": pkg_dir.joinpath('www'),
"default_filename": "index.html",
}),
(r'/(?:image)/(.*)', tornado.web.StaticFileHandler, {'path': pkg_dir.joinpath('image')}),
(r'/(?:css)/(.*)', tornado.web.StaticFileHandler, {'path': pkg_dir.joinpath('css')}),
(r'/(?:js)/(.*)', tornado.web.StaticFileHandler, {'path': pkg_dir.joinpath('js')})
],
template_path=pkg_dir.joinpath('www'),
debug=True,
camera=camera_class(),
sockets=[],
timers=[],
stream_mode=mode,
)
streamer_class.start(application=app)
server = app.listen(port, address)
tornado_utils.log_url(server)
try:
tornado.ioloop.IOLoop.current().start()
finally:
streamer_class.stop()
tornado_utils.stop_application(app.settings['sockets'])
if __name__ == "__main__":
main()