This file contains hidden or 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
from timeit import timeit | |
import asyncio | |
import requests | |
from threading import Thread | |
import aiohttp | |
client = aiohttp.ClientSession() | |
This file contains hidden or 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
import asyncio | |
import websockets | |
from websockets.exceptions import ConnectionClosed | |
from time import sleep | |
async def connect(): | |
while True: | |
try: | |
async with websockets.connect('ws://localhost:9999') as websocket: |
This file contains hidden or 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
backcall==0.1.0 | |
decorator==4.3.0 | |
hiredis==0.2.0 | |
httptools==0.0.11 | |
ipdb==0.11 | |
ipython==6.5.0 | |
ipython-genutils==0.2.0 | |
jedi==0.12.1 | |
parso==0.3.1 | |
pexpect==4.6.0 |
This file contains hidden or 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
import asyncio | |
import websockets | |
async def ws_handler(websocket, path): | |
async for message in websocket: | |
print(message) | |
if message == 'ping': | |
await websocket.send('pong') | |
This file contains hidden or 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
import asyncio | |
import websockets | |
async def heartbeat(websocket): | |
while True: | |
await asyncio.sleep(1) | |
await websocket.send('💚') | |
async def ws_handler(websocket, path): |
This file contains hidden or 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
async def beat1(websocket): | |
while True: | |
await asyncio.sleep(1) | |
await websocket.send('💚') | |
async def beat2(websocket): | |
while True: | |
await asyncio.sleep(2) | |
await websocket.send('💓') |
This file contains hidden or 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
async def beat1(websocket): | |
while True: | |
await asyncio.sleep(1) | |
await websocket.send('💚') | |
async def beat2(websocket): | |
while True: | |
await asyncio.sleep(2) | |
await websocket.send('💓') | |
This file contains hidden or 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
from aioredis.pubsub import Receiver | |
REDIS_URL = 'localhost' | |
REDIS_PORT = 6379 | |
async def redis_relay(websocket): | |
conn = aioredis.create_connection((REDIS_URL, REDIS_PORT)) | |
receiver = Receiver() | |
connection.execute_pubsub('subscribe', receiver.channel(‘updates’)) | |
while (await receiver.wait_message()): |
This file contains hidden or 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
import asyncio | |
async def client(message): | |
message = message + '\n' + message + '\n' | |
reader, writer = await asyncio.open_connection( | |
'127.0.0.1', 8888) | |
print(f'Send: {message!r}') | |
writer.write(message.encode()) | |
while True: |
This file contains hidden or 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
import asyncio | |
import random | |
async def handle_echo(reader, writer): | |
while True: | |
data = await reader.readline() | |
message = data.decode() | |
addr = writer.get_extra_info('peername') | |
print(f"Received {message!r} from {addr!r}") | |
message = f'pong{random.randint(0,10)}\n' |