diff --git a/daphne/cli.py b/daphne/cli.py index 3d8b1ab..1a4519e 100755 --- a/daphne/cli.py +++ b/daphne/cli.py @@ -33,6 +33,13 @@ class CommandLineInterface(object): help='The host/address to bind to', default="127.0.0.1", ) + self.parser.add_argument( + '-u', + '--unix-socket', + dest='unix_socket', + help='Bind to a UNIX socket rather than a TCP host/port', + default=None, + ) self.parser.add_argument( '-v', '--verbosity', @@ -83,14 +90,14 @@ class CommandLineInterface(object): channel_layer = getattr(channel_layer, bit) # Run server logger.info( - "Starting server on %s:%s, channel layer %s", - args.host, - args.port, + "Starting server at %s, channel layer %s", + (args.unix_socket if args.unix_socket else "%s:%s" % (args.host, args.port)), args.channel_layer, ) Server( channel_layer=channel_layer, host=args.host, port=args.port, + unix_socket=args.unix_socket, http_timeout=args.http_timeout, ).run() diff --git a/daphne/server.py b/daphne/server.py index 84f9d1d..6a06dc3 100755 --- a/daphne/server.py +++ b/daphne/server.py @@ -13,6 +13,7 @@ class Server(object): channel_layer, host="127.0.0.1", port=8000, + unix_socket=None, signal_handlers=True, action_logger=None, http_timeout=120, @@ -21,6 +22,7 @@ class Server(object): self.channel_layer = channel_layer self.host = host self.port = port + self.unix_socket = unix_socket self.signal_handlers = signal_handlers self.action_logger = action_logger self.http_timeout = http_timeout @@ -35,7 +37,10 @@ class Server(object): timeout=self.http_timeout, websocket_timeout=self.websocket_timeout, ) - reactor.listenTCP(self.port, self.factory, interface=self.host) + if self.unix_socket: + reactor.listenUNIX(self.unix_socket, 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)