Skip to content

Instantly share code, notes, and snippets.

Last active Sep 3, 2020
What would you like to do?
Start a Tornado static file server in a given directory. To start the server in the current directory: `tserv .`. Then go to `http://localhost:8000` to browse the directory.
#!/usr/bin/env python
Starts a Tornado static file server in a given directory.
To start the server in the current directory:
tserv .
Then go to http://localhost:8000 to browse the directory.
Use the --prefix option to add a prefix to the served URL,
for example to match GitHub Pages' URL scheme:
tserv . --prefix=jiffyclub
Then go to http://localhost:8000/jiffyclub/ to browse.
Use the --port option to change the port on which the server listens.
from __future__ import print_function
import os
import sys
from argparse import ArgumentParser
import tornado.ioloop
import tornado.web
class Handler(tornado.web.StaticFileHandler):
def parse_url_path(self, url_path):
if not url_path or url_path.endswith('/'):
url_path = url_path + 'index.html'
return url_path
def mkapp(prefix=''):
if prefix:
path = '/' + prefix + '/(.*)'
path = '/(.*)'
application = tornado.web.Application([
(path, Handler, {'path': os.getcwd()}),
], debug=True)
return application
def start_server(prefix='', port=8000):
app = mkapp(prefix)
def parse_args(args=None):
parser = ArgumentParser(
'Start a Tornado server to serve static files out of a '
'given directory and with a given prefix.'))
'-f', '--prefix', type=str, default='',
help='A prefix to add to the location from which pages are served.')
'-p', '--port', type=int, default=8000,
help='Port on which to run server.')
'dir', help='Directory from which to serve files.')
return parser.parse_args(args)
def main(args=None):
args = parse_args(args)
print('Starting server on port {}'.format(args.port))
start_server(prefix=args.prefix, port=args.port)
if __name__ == '__main__':
Copy link

HarryTanME commented Oct 15, 2017

This works like a charm. thanks for sharing this gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment