Created
March 19, 2019 04:28
-
-
Save kobayashi/2138c60ab400f5a31561359fad49b8a6 to your computer and use it in GitHub Desktop.
responder with websocket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import asyncio | |
import websockets | |
async def client(): | |
async with websockets.connect('ws://127.0.0.1:5042/ws') as websocket: | |
await websocket.send("KOBAYASHI") | |
response = await websocket.recv() | |
print(response) | |
asyncio.get_event_loop().run_until_complete(client()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import time | |
import responder | |
api = responder.API() | |
@api.route('/ws', websocket=True) | |
async def websocket(ws): | |
def sleep(name, s=5): | |
time.sleep(s) | |
return "Hello {}".format(name) | |
await ws.accept() | |
while True: | |
name = await ws.receive_text() | |
re = sleep(name) | |
await ws.send_text(re) | |
await ws.close() | |
if __name__ == '__main__': | |
api.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment