From 113cc0cc0c00d7834c07c2ee7b5ffd890bf3d436 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 2 Jun 2015 11:43:28 +0530 Subject: [PATCH] Make the interface/port to listen on an option --- src/calibre/srv/loop.py | 3 +-- src/calibre/srv/opts.py | 10 ++++++++++ src/calibre/srv/tests/base.py | 3 ++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/calibre/srv/loop.py b/src/calibre/srv/loop.py index 6cf48acb75..3f1f3d148e 100644 --- a/src/calibre/srv/loop.py +++ b/src/calibre/srv/loop.py @@ -274,7 +274,6 @@ class ServerLoop(object): def __init__( self, handler, - bind_address=('localhost', 8080), opts=None, # A calibre logging object. If None, a default log that logs to # stdout is used @@ -285,7 +284,7 @@ class ServerLoop(object): self.opts = opts or Options() self.log = log or ThreadSafeLog(level=ThreadSafeLog.DEBUG) - ba = tuple(bind_address) + ba = (opts.listen_on, int(opts.port)) if not ba[0]: # AI_PASSIVE does not work with host of '' or None ba = ('0.0.0.0', ba[1]) diff --git a/src/calibre/srv/opts.py b/src/calibre/srv/opts.py index 7e17f7a9a4..462b31ced5 100644 --- a/src/calibre/srv/opts.py +++ b/src/calibre/srv/opts.py @@ -56,6 +56,16 @@ raw_options = ( 'worker_count', 10, None, + 'The port on which to listen for connections', + 'port', 8080, + None, + + 'The interface on which to listen for connections', + 'listen_on', '0.0.0.0', + 'The default is to listen on all available interfaces. You can change this to, for' + ' example, "127.0.0.1" to only listen for connections from the local machine, or' + ' to "::" to listen to all incoming IPv6 and IPv4 connections.', + 'Use zero copy file transfers for increased performance', 'use_sendfile', True, 'This will use zero-copy in-kernel transfers when sending files over the network,' diff --git a/src/calibre/srv/tests/base.py b/src/calibre/srv/tests/base.py index 416bf2b66f..a5c4422fc0 100644 --- a/src/calibre/srv/tests/base.py +++ b/src/calibre/srv/tests/base.py @@ -38,10 +38,11 @@ class TestServer(Thread): from calibre.srv.loop import ServerLoop from calibre.srv.http_response import create_http_handler kwargs['shutdown_timeout'] = kwargs.get('shutdown_timeout', 0.1) + kwargs['listen_on'] = kwargs.get('listen_on', 'localhost') + kwargs['port'] = kwargs.get('port', 0) self.loop = ServerLoop( create_http_handler(handler), opts=Options(**kwargs), - bind_address=('localhost', 0), log=TestLog(level=ThreadSafeLog.WARN), ) self.log = self.loop.log