我的简单异步 Web 框架
项目描述
网络宠物
python的简单异步Web框架
这是我的宠物项目,它不假装是认真的
链接
简单用法示例
from webpet.application import ASGIApplication
from webpet.conf import Configuration
from webpet.response import HTTPResponse
from webpet.routers import HTTPRouter, URL
from webpet.views import View, LongPoolView, TemplateView
import asyncio
import json
import random
import os
BASE_DIR = os.getcwd() + '/'
class Index(TemplateView):
template_name = 'index.html'
class Aboba(View):
async def get(self):
raise ValueError()
class TestLong(LongPoolView):
async def get(self):
await self.open(status_code=200, headers=[(b'Content-type', b'application/json')])
data = []
ticks = 0
while True:
ticks += 1
number = random.randint(0, 1000)
data.append(number)
if number >= 500:
await self.send(HTTPResponse(
json.dumps({
'data': data,
'ticks': ticks
})
))
break
else:
await asyncio.sleep(1)
router = HTTPRouter(routes=[
URL('/', Index),
URL('/aboba', Aboba),
URL('/longpool', TestLong)
])
configuration = Configuration()
configuration.router = router
configuration.templates_dir = BASE_DIR + 'templates/'
app = ASGIApplication()
使用 daphne 运行
pip install daphne
daphne <application_name>:<ASGIApplication instance>
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。
源分布
webpet-0.1.1.tar.gz
(3.5 kB
查看哈希)
内置分布
webpet-0.1.1-py3-none-any.whl
(3.9 kB
查看哈希)