From a20ad99638bfba9576e3dea9e5f8f0a452c33d01 Mon Sep 17 00:00:00 2001 From: Stephen Sadowski Date: Fri, 19 Oct 2018 13:33:01 -0500 Subject: [PATCH] Added documentation for AF_INET6 and AF_UNIX socket usage --- docs/index.rst | 1 + docs/sanic/sockets.rst | 66 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 docs/sanic/sockets.rst diff --git a/docs/index.rst b/docs/index.rst index aaa296ee..fb8a45c4 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -21,6 +21,7 @@ Guides sanic/streaming sanic/class_based_views sanic/custom_protocol + sanic/sockets sanic/ssl sanic/logging sanic/testing diff --git a/docs/sanic/sockets.rst b/docs/sanic/sockets.rst new file mode 100644 index 00000000..847fded4 --- /dev/null +++ b/docs/sanic/sockets.rst @@ -0,0 +1,66 @@ +Sockets +======= + +Sanic can use the python +`socket module `_ to accommodate +non IPv4 sockets. + +IPv6 example: + +.. code:: python + + from sanic import Sanic + from sanic.response import json + import socket + + sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) + sock.bind(('::', 7777)) + + app = Sanic() + + + @app.route("/") + async def test(request): + return json({"hello": "world"}) + + if __name__ == "__main__": + app.run(sock=sock) + +to test IPv6 ``curl -g -6 "http://[::1]:7777/"`` + + +UNIX socket example: + +.. code:: python + + import signal + import sys + import socket + import os + from sanic import Sanic + from sanic.response import json + + + server_socket = '/tmp/sanic.sock' + + sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + sock.bind(server_socket) + + app = Sanic() + + + @app.route("/") + async def test(request): + return json({"hello": "world"}) + + + def signal_handler(sig, frame): + print('Exiting') + os.unlink(server_socket) + sys.exit(0) + + + if __name__ == "__main__": + app.run(sock=sock) + +to test UNIX: ``curl -v --unix-socket /tmp/sanic.sock http://localhost/hello``