-
Notifications
You must be signed in to change notification settings - Fork 3
/
tserv.py
109 lines (88 loc) · 3.1 KB
/
tserv.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
"""tsver.py
Saisoku is a Python module that helps you build complex pipelines of batch file copying jobs.
See README.md or https://github.com/shirosaidev/saisoku
for more information.
Author: shirosai <[email protected]>
Copyright (C) Chris Park 2019
saisoku is released under the Apache 2.0 license. See
LICENSE for the full license text.
"""
import os
import sys
from argparse import ArgumentParser
from scandir import scandir
import logging
import signal
import tornado.ioloop
import tornado.web
import tornado.template
import tornado.httpserver
logging.basicConfig(level=logging.DEBUG)
class IndexHandler(tornado.web.RequestHandler):
SUPPORTED_METHODS = ['GET']
def get(self, path):
""" GET method to list contents of directory or
write index page if index.html exists."""
# remove heading slash
path = path[1:]
for index in ['index.html', 'index.htm']:
index = os.path.join(path, index)
if os.path.exists(index):
with open(index, 'rb') as f:
self.write(f.read())
self.finish()
return
html = self.generate_index(path)
self.write(html)
self.finish()
def generate_index(self, path):
""" generate index html page, list all files and dirs.
"""
if path:
files = [item for item in scandir(path)]
else:
files = [item for item in scandir('.')]
files = [(item.name + '/', 0) if item.is_dir() else (item.name, str(item.stat().st_size)) for item in files]
html_template = """
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html>
<title>Directory listing for /{{ path }}</title>
<body>
<h2>Directory listing for /{{ path }}</h2>
<hr>
<ul>
{% for filename, size in files %}
<li><a title="{{ size }}" href="{{ filename }}">{{ filename }}</a>
{% end %}
</ul>
<hr>
</body>
</html>
"""
t = tornado.template.Template(html_template)
return t.generate(files=files, path=path)
class StaticFileHandler(tornado.web.StaticFileHandler):
def write(self, chunk):
super(StaticFileHandler, self).write(chunk)
#logging.debug('write called')
self.flush()
def stop_server(signum, frame):
tornado.ioloop.IOLoop.instance().stop()
logging.info('Stopped!')
def start_http_server(args):
os.chdir(args.dir)
logging.debug('cwd: %s' % args.dir)
try:
application = tornado.web.Application([
(r'(.*)/$', IndexHandler,),
(r'/(.*)$', StaticFileHandler, {'path': args.dir}),
])
signal.signal(signal.SIGINT, stop_server)
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(args.port)
logging.info('Starting HTTP server on 0.0.0.0 port %d ...' % args.port)
tornado.ioloop.IOLoop.instance().start()
except KeyboardInterrupt:
signal.signal(signal.SIGINT, stop_server)