Make the interface/port to listen on an option

This commit is contained in:
Kovid Goyal 2015-06-02 11:43:28 +05:30
parent c75ec6358d
commit 113cc0cc0c
3 changed files with 13 additions and 3 deletions

View File

@ -274,7 +274,6 @@ class ServerLoop(object):
def __init__( def __init__(
self, self,
handler, handler,
bind_address=('localhost', 8080),
opts=None, opts=None,
# A calibre logging object. If None, a default log that logs to # A calibre logging object. If None, a default log that logs to
# stdout is used # stdout is used
@ -285,7 +284,7 @@ class ServerLoop(object):
self.opts = opts or Options() self.opts = opts or Options()
self.log = log or ThreadSafeLog(level=ThreadSafeLog.DEBUG) self.log = log or ThreadSafeLog(level=ThreadSafeLog.DEBUG)
ba = tuple(bind_address) ba = (opts.listen_on, int(opts.port))
if not ba[0]: if not ba[0]:
# AI_PASSIVE does not work with host of '' or None # AI_PASSIVE does not work with host of '' or None
ba = ('0.0.0.0', ba[1]) ba = ('0.0.0.0', ba[1])

View File

@ -56,6 +56,16 @@ raw_options = (
'worker_count', 10, 'worker_count', 10,
None, 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 zero copy file transfers for increased performance',
'use_sendfile', True, 'use_sendfile', True,
'This will use zero-copy in-kernel transfers when sending files over the network,' 'This will use zero-copy in-kernel transfers when sending files over the network,'

View File

@ -38,10 +38,11 @@ class TestServer(Thread):
from calibre.srv.loop import ServerLoop from calibre.srv.loop import ServerLoop
from calibre.srv.http_response import create_http_handler from calibre.srv.http_response import create_http_handler
kwargs['shutdown_timeout'] = kwargs.get('shutdown_timeout', 0.1) 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( self.loop = ServerLoop(
create_http_handler(handler), create_http_handler(handler),
opts=Options(**kwargs), opts=Options(**kwargs),
bind_address=('localhost', 0),
log=TestLog(level=ThreadSafeLog.WARN), log=TestLog(level=ThreadSafeLog.WARN),
) )
self.log = self.loop.log self.log = self.loop.log