install sanic:python3 -m pip install sanic
example
from sanic import sanic
from sanic.response import text
async def test(request):
return text('hello world!')
路由允許使用者為不同的url端點指定處理程式函式。
demo:
from sanic.response import jsonurl 被訪問(伺服器的基本url),最終'/'被路由器匹配到處理程式函式,測試,然後返回乙個json物件。async def test(request):
return json()
要指定乙個引數,可以用像這樣的角引號包圍它。請求引數將作為關鍵字引數傳遞給路線處理程式函式。
demo
from sanic.response import text
async def tag_handler(request, tag):
return text('tag - {}'.format(tag))
為引數指定型別,在引數名後面新增(:型別)。如果引數不匹配指定的型別,sanic將丟擲乙個不存在的異常,導致乙個404頁面
demo:
from sanic.response import text
async def integer_handler(request, integer_arg):
return text('integer - {}'.format(integer_arg))
async def number_handler(request, number_arg):
return text('number - {}'.format(number_arg))
async def person_handler(request, name):
return text('person - {}'.format(name))
async def folder_handler(request, folder_id):
return text('folder - {}'.format(folder_id))
路由裝飾器接受乙個可選的引數,方法,它允許處理程式函式與列表中的任何http方法一起工作。
demo_1
from sanic.response import text
async def post_handler(request):
return text('post request - {}'.format(request.json))
async def get_handler(request):
return text('get request - {}'.format(request.args))
demo_2
from sanic.response import text
async def post_handler(request):
return text('post request - {}'.format(request.json))
async def get_handler(request):
return text('get request - {}'.format(request.args))
from sanic.response import text
# define the handler functions
async def handler1(request):
return text('ok')
async def handler2(request, name):
return text('folder - {}'.format(name))
async def person_handler2(request, name):
return text('person - {}'.format(name))
# add each handler function as a route
sanic提供了乙個urlfor方法,根據處理程式方法名生成url。避免硬編碼url路徑到您的應用程式
demo
async def index(request):
# generate a url for the endpoint `post_handler`
# the url is `/posts/5`, redirect to it
return redirect(url)
async def post_handler(request, post_id):
return text('post - {}'.format(post_id))
notice:
# /posts/5?arg_one=one&arg_two=two
# /posts/5?arg_one=one&arg_one=two
websocket 可以通過裝飾路由實現
demo:
async def feed(request, ws):
while true:
data = 'hello!'
print('sending: ' + data)
await ws.send(data)
data = await ws.recv()
print('received: ' + data)
另外,新增 websocket 路由方法可以代替裝飾器
async def feed(request, ws):
pass
from sanic import response
def handle_request(request):
return response.text('hello world!')
from sanic import response
def handle_request(request):
return response.html('hello world!
')
from sanic import response
def handle_request(request):
return response.json()
from sanic import response
async def handle_request(request):
return await response.file('/srv/www/whatever.png')
streamingfrom sanic import response
async def index(request):
async def streaming_fn(response):
response.write('foo')
response.write('bar')
return response.stream(streaming_fn, content_type='text/plain')
對於大檔案,檔案和流的組合
from sanic import response
async def handle_request(request):
return await response.file_stream('/srv/www/whatever.png')
from sanic import response
def handle_request(request):
return response.redirect('/json')
沒有進行編碼的響應
from sanic import response
def handle_request(request):
return response.raw(『 raw data 』)
要修改頭或狀態**,將標題或狀態引數傳遞給這些函式
from sanic import response
def handle_request(request):
return response.json(
,headers=,
status=200
)
更多的內容:sanic 中文文件 Pentaho bi 中文文件
目錄 building and debugging pentaho with eclipse zh cn 1.5.4 getting started with the bi platform zh cn 1.5.4 pentaho building components 1.5.4 pentaho ...
vim 中文文件
安裝中文幫助文件之前首先執行下列操作 在home目錄下列新建資料夾 vim vim是乙個隱藏檔案,不要漏了 vim plugin vim目錄下的plugin資料夾 vim doc vim目錄下的doc資料夾 vim syntax vim目錄下的syntax資料夾 在home目錄下新建隱藏檔案 vim...
sklearn中文文件
apachecn cwiki 位址為 scikit learn 0.18 中文文件 sklearn 基於 python 語言的,簡單高效的資料探勘和資料分析工具,建立在 numpy,scipy 和 matplotlib 上。以下是 sklearn 官方文件中文版,如果你也有興趣,歡迎來一起來維護迭代...