From 28994f4b647be01bb162af6dedb2b1b3d3660c52 Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Sat, 20 May 2017 02:15:45 -0700 Subject: [PATCH 1/2] update todo --- README.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/README.rst b/README.rst index 64015d2c..8a7b2706 100644 --- a/README.rst +++ b/README.rst @@ -86,7 +86,6 @@ Documentation TODO ---- - * Streamed file processing * http2 Limitations From 52b0254ec682498166e58228d582296117f16532 Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Sun, 21 May 2017 03:14:58 -0700 Subject: [PATCH 2/2] unix socket support; fixes #700 --- examples/unix_socket.py | 23 +++++++++++++++++++++++ sanic/app.py | 8 +++++--- sanic/server.py | 14 ++++++++------ 3 files changed, 36 insertions(+), 9 deletions(-) create mode 100644 examples/unix_socket.py 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 29b8c71c..cfdef3ab 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -521,7 +521,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): @@ -580,7 +580,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): @@ -629,11 +629,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(