Skip to content

Commit

Permalink
fix #1682 -- allow IPv6 in nikola serve
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed May 4, 2015
1 parent c231f17 commit 27b1cf4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Expand Up @@ -4,6 +4,7 @@ New in master
Features
--------

* Allow IPv6 in `nikola serve` (Issue #1682)
* Set default new site URL to https://example.com/
* Plugins can manipulate task dependencies (Issue #1679)
* LINK_CHECK_WHITELIST now works with output relative and full fs paths
Expand Down
32 changes: 27 additions & 5 deletions nikola/plugins/command/serve.py
Expand Up @@ -26,6 +26,7 @@

from __future__ import print_function
import os
import socket
import webbrowser
try:
from BaseHTTPServer import HTTPServer
Expand All @@ -38,6 +39,11 @@
from nikola.utils import get_logger


class IPv6Server(HTTPServer):
"""An IPv6 HTTPServer."""
address_family = socket.AF_INET6


class CommandServe(Command):
"""Start test server."""

Expand All @@ -53,15 +59,15 @@ class CommandServe(Command):
'long': 'port',
'default': 8000,
'type': int,
'help': 'Port nummber (default: 8000)',
'help': 'Port number (default: 8000)',
},
{
'name': 'address',
'short': 'a',
'long': 'address',
'type': str,
'default': '',
'help': 'Address to bind (default: 0.0.0.0 – all local interfaces)',
'help': 'Address to bind (default: 0.0.0.0 – all local IPv4 interfaces)',
},
{
'name': 'browser',
Expand All @@ -70,7 +76,15 @@ class CommandServe(Command):
'type': bool,
'default': False,
'help': 'Open the test server in a web browser',
}
},
{
'name': 'ipv6',
'short': '6',
'long': 'ipv6',
'type': bool,
'default': False,
'help': 'Use IPv6',
},
)

def _execute(self, options, args):
Expand All @@ -81,8 +95,16 @@ def _execute(self, options, args):
self.logger.error("Missing '{0}' folder?".format(out_dir))
else:
os.chdir(out_dir)
httpd = HTTPServer((options['address'], options['port']),
OurHTTPRequestHandler)
if '[' in options['address']:
options['address'] = options['address'].strip('[').strip(']')
OurHTTP = IPv6Server
elif options['ipv6']:
OurHTTP = IPv6Server
else:
OurHTTP = HTTPServer

httpd = OurHTTP((options['address'], options['port']),
OurHTTPRequestHandler)
sa = httpd.socket.getsockname()
self.logger.info("Serving HTTP on {0} port {1}...".format(*sa))
if options['browser']:
Expand Down

0 comments on commit 27b1cf4

Please sign in to comment.