python - Using Websocket in Pyramid using Python3 -


is there way use websockets in pyramid using python 3. want use live-updating tables when there data changes on server.

i thought of using long-polling, don't think best way.

any comments or ideas?

https://github.com/housleyjk/aiopyramid works me. see documentation websocket http://aiopyramid.readthedocs.io/features.html#websockets

upd:

websocket server pyramid environment.

import aiohttp import asyncio aiohttp import web webtest import testapp pyramid.config import configurator pyramid.response import response  async def websocket_handler(request):      ws = web.websocketresponse()     await ws.prepare(request)      while not ws.closed:         msg = await ws.receive()          if msg.tp == aiohttp.msgtype.text:             if msg.data == 'close':                 await ws.close()             else:                 hello = testapp(request.app.pyramid).get('/')                 ws.send_str(hello.text)         elif msg.tp == aiohttp.msgtype.close:             print('websocket connection closed')         elif msg.tp == aiohttp.msgtype.error:             print('ws connection closed exception %s' %                   ws.exception())         else:             ws.send_str('hi')      return ws   def hello(request):     return response('hello world!')  async def init(loop):     app = web.application(loop=loop)     app.router.add_route('get', '/{name}', websocket_handler)     config = configurator()     config.add_route('hello_world', '/')     config.add_view(hello, route_name='hello_world')     app.pyramid = config.make_wsgi_app()      srv = await loop.create_server(app.make_handler(),                                    '127.0.0.1', 8080)     print("server started @ http://127.0.0.1:8080")     return srv  loop = asyncio.get_event_loop() loop.run_until_complete(init(loop)) try:     loop.run_forever() except keyboardinterrupt:     pass 

websocket client:

import asyncio import aiohttp  session = aiohttp.clientsession()   async def client():     ws = await session.ws_connect('http://0.0.0.0:8080/foo')      while true:         ws.send_str('hi')         await asyncio.sleep(2)         msg = await ws.receive()          if msg.tp == aiohttp.msgtype.text:             print('msg: ', msg)             if msg.data == 'close':                 await ws.close()                 break             else:                 ws.send_str(msg.data + '/client')         elif msg.tp == aiohttp.msgtype.closed:             break         elif msg.tp == aiohttp.msgtype.error:             break  loop = asyncio.get_event_loop() loop.run_until_complete(client()) loop.close() 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -