sanic/examples/custom_protocol.py

29 lines
603 B
Python
Raw Normal View History

from sanic import Sanic
from sanic.server import HttpProtocol
from sanic.response import text
app = Sanic(__name__)
class CustomHttpProtocol(HttpProtocol):
def write_response(self, response):
if isinstance(response, str):
response = text(response)
self.transport.write(
response.output(self.request.version)
)
self.transport.close()
2016-12-26 14:54:59 +00:00
@app.route('/')
async def string(request):
return 'string'
2016-12-26 14:54:59 +00:00
@app.route('/1')
async def response(request):
return text('response')
app.run(host='0.0.0.0', port=8000, protocol=CustomHttpProtocol)