async def echo(websocket): try:
async for message in websocket: try:
log.info(f'got request {message}') except Exception as e:
log.error(f'error {e} getting path from {message}')
await websocket.send(message) except ConnectionClosedError: pass
async def on_async_conn(conn):
rpath = str(conn.path)
pcomps = rpath[1:].split('/') if len(pcomps) == 0:
pcomps = ['echo'] # default handler
log.info(f'connection for {pcomps}') if pcomps[0] == 'echo':
log.info(f'/echo endpoint') for message in await conn.recv():
await conn.send(message) elif pcomps[0] == 'text':
await conn.send('hello!') elif pcomps[0] == 'file': if len(pcomps) < 2:
conn.close(code=4999, reason='unknown file') return
fpath = os.path.join('../', pcomps[1]) ifnot os.path.exists(fpath):
conn.close(code=4999, reason='file not found') return
bufsize = 0 if len(pcomps) > 2:
bufsize = int(pcomps[2]) if bufsize <= 0:
bufsize = 16*1024
delay_ms = 0 if len(pcomps) > 3:
delay_ms = int(pcomps[3])
n = 1 if len(pcomps) > 4:
n = int(pcomps[4]) for _ in range(n): with open(fpath, 'r+b') as fd: whileTrue:
buf = fd.read(bufsize) if buf isNoneor len(buf) == 0: break
await conn.send(buf) if delay_ms > 0:
time.sleep(delay_ms/1000) else:
log.info(f'unknown endpoint: {rpath}')
await conn.close(code=4999, reason='path unknown')
await conn.close(code=1000, reason='')
async def run_server(port):
log.info(f'starting server on port {port}')
async with ws_server.serve(ws_handler=on_async_conn,
host="localhost", port=port):
await asyncio.Future()
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.