# Buggy code
def _livereload(host, port, config, builder, site_dir):
    # We are importing here for anyone that has issues with livereload. Even if
    # this fails, the --no-livereload alternative should still work.
    from livereload import Server
    import livereload.handlers

    class LiveReloadServer(Server):
        def get_web_handlers(self, script):
            handlers = super(LiveReloadServer, self).get_web_handlers(script)
            # replace livereload handler
            return [
                (
                    handlers[0][0],
                    _get_handler(site_dir, livereload.handlers.StaticFileHandler),
                    handlers[0][2],
                )
            ]

    server = LiveReloadServer()

    # Watch the documentation files, the config file and the theme files.
    server.watch(config["docs_dir"], builder)
    server.watch(config["config_file_path"], builder)

    for d in config["theme_dir"]:
        server.watch(d, builder)

    server.serve(root=site_dir, host=host, port=int(port), restart_delay=0)


# Traceback (ValueError)
INFO    -  Building documentation...
INFO    -  Cleaning site directory
Traceback (most recent call last):
File "/home/sybren/.virtualenvs/flamenco/bin/mkdocs", line 11, in <module>
sys.exit(cli())
File "/home/sybren/.virtualenvs/flamenco/lib/python3.6/site-packages/click/core.py", line 722, in __call__
return self.main(*args, **kwargs)
File "/home/sybren/.virtualenvs/flamenco/lib/python3.6/site-packages/click/core.py", line 697, in main
rv = self.invoke(ctx)
File "/home/sybren/.virtualenvs/flamenco/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/sybren/.virtualenvs/flamenco/lib/python3.6/site-packages/click/core.py", line 895, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/sybren/.virtualenvs/flamenco/lib/python3.6/site-packages/click/core.py", line 535, in invoke
return callback(*args, **kwargs)
File "/home/sybren/.virtualenvs/flamenco/lib/python3.6/site-packages/mkdocs/__main__.py", line 127, in serve_command
livereload=livereload
File "/home/sybren/.virtualenvs/flamenco/lib/python3.6/site-packages/mkdocs/commands/serve.py", line 116, in serve
_livereload(host, port, config, builder, tempdir)
File "/home/sybren/.virtualenvs/flamenco/lib/python3.6/site-packages/mkdocs/commands/serve.py", line 55, in _livereload
server.serve(root=site_dir, host=host, port=int(port), restart_delay=0)
ValueError: invalid literal for int() with base 10: ':0]:8081'

# Fixed code
def _livereload(host, port, config, builder, site_dir):
    # We are importing here for anyone that has issues with livereload. Even if
    # this fails, the --no-livereload alternative should still work.
    from livereload import Server
    import livereload.handlers

    class LiveReloadServer(Server):
        def get_web_handlers(self, script):
            handlers = super(LiveReloadServer, self).get_web_handlers(script)
            # replace livereload handler
            return [
                (
                    handlers[0][0],
                    _get_handler(site_dir, livereload.handlers.StaticFileHandler),
                    handlers[0][2],
                )
            ]

    server = LiveReloadServer()

    # Watch the documentation files, the config file and the theme files.
    server.watch(config["docs_dir"], builder)
    server.watch(config["config_file_path"], builder)

    for d in config["theme_dir"]:
        server.watch(d, builder)

    server.serve(root=site_dir, host=host, port=port, restart_delay=0)