From 0e1bce17141a262263400b8974b1f161d107833a Mon Sep 17 00:00:00 2001 From: Sean Mc Allister Date: Wed, 13 Jul 2016 14:07:57 +0200 Subject: [PATCH 1/3] add file_descriptor keword argument to daphne server and instruct twisted reactor to use the file descriptor --- daphne/server.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/daphne/server.py b/daphne/server.py index 04757f7..fb7ae2d 100755 --- a/daphne/server.py +++ b/daphne/server.py @@ -1,6 +1,6 @@ import logging from twisted.internet import reactor - +import socket from .http_protocol import HTTPFactory logger = logging.getLogger(__name__) @@ -14,6 +14,7 @@ class Server(object): host="127.0.0.1", port=8000, unix_socket=None, + file_descriptor=None, signal_handlers=True, action_logger=None, http_timeout=120, @@ -26,6 +27,7 @@ class Server(object): self.host = host self.port = port self.unix_socket = unix_socket + self.file_descriptor = file_descriptor self.signal_handlers = signal_handlers self.action_logger = action_logger self.http_timeout = http_timeout @@ -48,8 +50,13 @@ class Server(object): ) if self.unix_socket: reactor.listenUNIX(self.unix_socket, self.factory) + elif self.file_descriptor: + # socket returns the same socket if supplied with a fileno + sock = socket.socket(fileno=self.file_descriptor) + reactor.adoptStreamPort(self.file_descriptor, sock.family, self.factory) else: reactor.listenTCP(self.port, self.factory, interface=self.host) + reactor.callLater(0, self.backend_reader) reactor.callLater(2, self.timeout_checker) reactor.run(installSignalHandlers=self.signal_handlers) From 89686f9034f1382361d043bd32459d3facfdca12 Mon Sep 17 00:00:00 2001 From: Sean Mc Allister Date: Wed, 13 Jul 2016 14:09:14 +0200 Subject: [PATCH 2/3] expose file descriptor on command line --- daphne/cli.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/daphne/cli.py b/daphne/cli.py index cf075b1..ae301eb 100755 --- a/daphne/cli.py +++ b/daphne/cli.py @@ -41,6 +41,13 @@ class CommandLineInterface(object): help='Bind to a UNIX socket rather than a TCP host/port', default=None, ) + self.parser.add_argument( + '--fd', + type=int, + dest='file_descriptor', + help='Bind to a file descriptor rather than a TCP host/port or named unix socket', + default=None, + ) self.parser.add_argument( '-v', '--verbosity', @@ -133,6 +140,7 @@ class CommandLineInterface(object): host=args.host, port=args.port, unix_socket=args.unix_socket, + file_descriptor=args.file_descriptor, http_timeout=args.http_timeout, ping_interval=args.ping_interval, action_logger=AccessLogGenerator(access_log_stream) if access_log_stream else None, From 626b15e8c3eb06c62fcdb4ec47a72c04217f4b9d Mon Sep 17 00:00:00 2001 From: Sean Mc Allister Date: Thu, 14 Jul 2016 10:54:23 +0200 Subject: [PATCH 3/3] Update README.rst Added some more text to explain usage behind proxy servers and inside process managers --- README.rst | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.rst b/README.rst index 4ba1ca6..8c75c98 100644 --- a/README.rst +++ b/README.rst @@ -24,6 +24,21 @@ set a bind address and port (defaults to localhost, port 8000):: daphne -b 0.0.0.0 -p 8001 django_project.asgi:channel_layer +If you intend to run daphne behind a proxy server you can use UNIX +sockets to communicate between the two:: + + daphne -u /tmp/daphne.sock django_project.asgi:channel_layer + + +If daphne is being run inside a process manager such as +`Circus `_ you might +want it to bind to a file descriptor passed down from a parent process. +To achieve this you can use the --fd flag:: + + daphne --fd 5 django_project.asgi:channel_layer + +To see all available command line options run daphne with the *-h* flag. + Root Path (SCRIPT_NAME) -----------------------