fix one example and add one example (#1257)

This commit is contained in:
fanjindong 2018-07-11 16:42:34 +08:00 committed by Raphael Deem
parent f9b29fd7e7
commit becbc5f9ef
2 changed files with 50 additions and 5 deletions

View File

@ -1,7 +1,5 @@
from sanic import Sanic from sanic import Blueprint, Sanic
from sanic import Blueprint from sanic.response import file, json
from sanic.response import json
app = Sanic(__name__) app = Sanic(__name__)
blueprint = Blueprint('name', url_prefix='/my_blueprint') blueprint = Blueprint('name', url_prefix='/my_blueprint')
@ -19,7 +17,12 @@ async def foo2(request):
return json({'msg': 'hi from blueprint2'}) return json({'msg': 'hi from blueprint2'})
@blueprint3.websocket('/foo') @blueprint3.route('/foo')
async def index(request):
return await file('websocket.html')
@app.websocket('/feed')
async def foo3(request, ws): async def foo3(request, ws):
while True: while True:
data = 'hello!' data = 'hello!'

View File

@ -0,0 +1,42 @@
from sanic import Sanic
from sanic.views import HTTPMethodView
from sanic.response import text
app = Sanic('some_name')
class SimpleView(HTTPMethodView):
def get(self, request):
return text('I am get method')
def post(self, request):
return text('I am post method')
def put(self, request):
return text('I am put method')
def patch(self, request):
return text('I am patch method')
def delete(self, request):
return text('I am delete method')
class SimpleAsyncView(HTTPMethodView):
async def get(self, request):
return text('I am async get method')
async def post(self, request):
return text('I am async post method')
async def put(self, request):
return text('I am async put method')
app.add_route(SimpleView.as_view(), '/')
app.add_route(SimpleAsyncView.as_view(), '/async')
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8000, debug=True)