Skip to content

Instantly share code, notes, and snippets.

@aisk
Created October 24, 2016 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aisk/065dae7db395bcd934c292c1fcebb718 to your computer and use it in GitHub Desktop.
Save aisk/065dae7db395bcd934c292c1fcebb718 to your computer and use it in GitHub Desktop.
import sys
import time
import asyncio
import websockets
from datetime import datetime
last_connect_time = datetime.now()
@asyncio.coroutine
def connect():
url = sys.argv[1]
global last_connect_time
now = datetime.now()
print('connection sustained:', now - last_connect_time)
last_connect_time = now
result = yield from websockets.connect(url)
return result
@asyncio.coroutine
def hello():
msg = 'fuck!'
ws = yield from connect()
while True:
yield from ws.send(msg)
# print(datetime.now(), '>', msg)
try:
msg = yield from ws.recv()
except websockets.exceptions.ConnectionClosed as e:
print('error:', e)
ws = yield from connect()
# else:
# print(datetime.now(), '<', msg)
time.sleep(1)
ws.close()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(hello())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment