summaryrefslogtreecommitdiffstats
path: root/nikola/plugins/command/serve.py
diff options
context:
space:
mode:
authorLibravatarAgustin Henze <tin@sluc.org.ar>2015-07-08 07:35:02 -0300
committerLibravatarAgustin Henze <tin@sluc.org.ar>2015-07-08 07:35:02 -0300
commitb0b24795b24ee6809397fbbadf42f31f310a219f (patch)
tree46d05bb47460b4ec679211717c4ab07414b80d9c /nikola/plugins/command/serve.py
parent5ec02211214350ee558fd9f6bb052264fd24f75e (diff)
Imported Upstream version 7.6.0upstream/7.6.0
Diffstat (limited to 'nikola/plugins/command/serve.py')
-rw-r--r--nikola/plugins/command/serve.py44
1 files changed, 36 insertions, 8 deletions
diff --git a/nikola/plugins/command/serve.py b/nikola/plugins/command/serve.py
index de4f6e2..0e4d01f 100644
--- a/nikola/plugins/command/serve.py
+++ b/nikola/plugins/command/serve.py
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
-# Copyright © 2012-2014 Roberto Alsina and others.
+# Copyright © 2012-2015 Roberto Alsina and others.
# Permission is hereby granted, free of charge, to any
# person obtaining a copy of this software and associated
@@ -26,6 +26,7 @@
from __future__ import print_function
import os
+import socket
import webbrowser
try:
from BaseHTTPServer import HTTPServer
@@ -38,6 +39,11 @@ from nikola.plugin_categories import Command
from nikola.utils import get_logger
+class IPv6Server(HTTPServer):
+ """An IPv6 HTTPServer."""
+ address_family = socket.AF_INET6
+
+
class CommandServe(Command):
"""Start test server."""
@@ -53,7 +59,7 @@ class CommandServe(Command):
'long': 'port',
'default': 8000,
'type': int,
- 'help': 'Port nummber (default: 8000)',
+ 'help': 'Port number (default: 8000)',
},
{
'name': 'address',
@@ -61,7 +67,7 @@ class CommandServe(Command):
'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',
@@ -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):
@@ -81,19 +95,33 @@ class CommandServe(Command):
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(']')
+ ipv6 = True
+ OurHTTP = IPv6Server
+ elif options['ipv6']:
+ ipv6 = True
+ OurHTTP = IPv6Server
+ else:
+ ipv6 = False
+ 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']:
- server_url = "http://{0}:{1}/".format(*sa)
+ if ipv6:
+ server_url = "http://[{0}]:{1}/".format(*sa)
+ else:
+ server_url = "http://{0}:{1}/".format(*sa)
self.logger.info("Opening {0} in the default web browser...".format(server_url))
webbrowser.open(server_url)
try:
httpd.serve_forever()
except KeyboardInterrupt:
self.logger.info("Server is shutting down.")
- exit(130)
+ return 130
class OurHTTPRequestHandler(SimpleHTTPRequestHandler):