Merge pull request #742 from r0fls/700

changes required for unix socket support
This commit is contained in:
Raphael Deem 2017-05-22 19:29:32 -07:00 committed by GitHub
commit b447807b36
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

@ -522,7 +522,7 @@ class Sanic:
# Execution # 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, sock=None, workers=1, protocol=None,
backlog=100, stop_event=None, register_sys_signals=True, backlog=100, stop_event=None, register_sys_signals=True,
log_config=LOGGING): log_config=LOGGING):
@ -581,7 +581,7 @@ class Sanic:
"""gunicorn compatibility""" """gunicorn compatibility"""
return self 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, ssl=None, sock=None, protocol=None,
backlog=100, stop_event=None, backlog=100, stop_event=None,
log_config=LOGGING): log_config=LOGGING):
@ -630,11 +630,13 @@ class Sanic:
break break
return response 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, ssl=None, sock=None, workers=1, loop=None,
protocol=HttpProtocol, backlog=100, stop_event=None, protocol=HttpProtocol, backlog=100, stop_event=None,
register_sys_signals=True, run_async=False, has_log=True): register_sys_signals=True, run_async=False, has_log=True):
"""Helper function used by `run` and `create_server`.""" """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): if isinstance(ssl, dict):
# try common aliaseses # try common aliaseses

View File

@ -201,9 +201,10 @@ class HttpProtocol(asyncio.Protocol):
netlog.info('', extra={ netlog.info('', extra={
'status': response.status, 'status': response.status,
'byte': len(response.body), 'byte': len(response.body),
'host': '%s:%d' % (self.request.ip[0], self.request.ip[1]), 'host': '{0}:{1}'.format(self.request.ip[0],
'request': '%s %s' % (self.request.method, self.request.ip[1]),
self.request.url) 'request': '{0} {1}'.format(self.request.method,
self.request.url)
}) })
except AttributeError: except AttributeError:
log.error( log.error(
@ -242,9 +243,10 @@ class HttpProtocol(asyncio.Protocol):
netlog.info('', extra={ netlog.info('', extra={
'status': response.status, 'status': response.status,
'byte': -1, 'byte': -1,
'host': '%s:%d' % self.request.ip, 'host': '{0}:{1}'.format(self.request.ip[0],
'request': '%s %s' % (self.request.method, self.request.ip[1]),
self.request.url) 'request': '{0} {1}'.format(self.request.method,
self.request.url)
}) })
except AttributeError: except AttributeError:
log.error( log.error(