diff --git a/examples/unix_socket.py b/examples/unix_socket.py new file mode 100644 index 00000000..070074fe --- /dev/null +++ b/examples/unix_socket.py @@ -0,0 +1,23 @@ +from sanic import Sanic +from sanic import response +import socket +import sys +import os + +app = Sanic(__name__) + +@app.route("/test") +async def test(request): + return response.text("OK") + +if __name__ == '__main__': + server_address = './uds_socket' + # Make sure the socket does not already exist + try: + os.unlink(server_address) + except OSError: + if os.path.exists(server_address): + raise + sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + sock.bind(server_address) + app.run(sock=sock) diff --git a/sanic/app.py b/sanic/app.py index f3a023d8..5e8276c3 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -522,7 +522,7 @@ class Sanic: # Execution # -------------------------------------------------------------------- # - def run(self, host="127.0.0.1", port=8000, debug=False, ssl=None, + def run(self, host=None, port=None, debug=False, ssl=None, sock=None, workers=1, protocol=None, backlog=100, stop_event=None, register_sys_signals=True, log_config=LOGGING): @@ -581,7 +581,7 @@ class Sanic: """gunicorn compatibility""" return self - async def create_server(self, host="127.0.0.1", port=8000, debug=False, + async def create_server(self, host=None, port=None, debug=False, ssl=None, sock=None, protocol=None, backlog=100, stop_event=None, log_config=LOGGING): @@ -630,11 +630,13 @@ class Sanic: break return response - def _helper(self, host="127.0.0.1", port=8000, debug=False, + def _helper(self, host=None, port=None, debug=False, ssl=None, sock=None, workers=1, loop=None, protocol=HttpProtocol, backlog=100, stop_event=None, register_sys_signals=True, run_async=False, has_log=True): """Helper function used by `run` and `create_server`.""" + if sock is None: + host, port = host or "127.0.0.1", port or 8000 if isinstance(ssl, dict): # try common aliaseses diff --git a/sanic/server.py b/sanic/server.py index 02db6fe5..b8409efc 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -201,9 +201,10 @@ class HttpProtocol(asyncio.Protocol): netlog.info('', extra={ 'status': response.status, 'byte': len(response.body), - 'host': '%s:%d' % (self.request.ip[0], self.request.ip[1]), - 'request': '%s %s' % (self.request.method, - self.request.url) + 'host': '{0}:{1}'.format(self.request.ip[0], + self.request.ip[1]), + 'request': '{0} {1}'.format(self.request.method, + self.request.url) }) except AttributeError: log.error( @@ -242,9 +243,10 @@ class HttpProtocol(asyncio.Protocol): netlog.info('', extra={ 'status': response.status, 'byte': -1, - 'host': '%s:%d' % self.request.ip, - 'request': '%s %s' % (self.request.method, - self.request.url) + 'host': '{0}:{1}'.format(self.request.ip[0], + self.request.ip[1]), + 'request': '{0} {1}'.format(self.request.method, + self.request.url) }) except AttributeError: log.error(