unix socket support; fixes #700

This commit is contained in:
Raphael Deem 2017-05-21 03:14:58 -07:00
parent 6bcc0d3c7f
commit 52b0254ec6
3 changed files with 36 additions and 9 deletions

23
examples/unix_socket.py Normal file
View File

@ -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)

View File

@ -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

View File

@ -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(