Created
February 18, 2013 01:45
-
-
Save muellermartin/4974648 to your computer and use it in GitHub Desktop.
Python ws4py WebSocket Client
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 time | |
from ws4py.client.threadedclient import WebSocketClient | |
class SysMon(object): | |
def __init__(self, host): | |
self.running = False | |
self.client = SysMonWebSocketClient(host) | |
self.client.connect() | |
def run(self): | |
try: | |
self.running = True | |
i = 1 | |
while self.running: | |
if self.client.terminated: | |
print "Loop cycles: %s" % i | |
break | |
self.client.send("a") | |
time.sleep(0.15) | |
i += 1 | |
finally: | |
self.terminate() | |
def terminate(self): | |
self.running = False | |
if not self.client.terminated: | |
self.client.close() | |
self.client._th.join() | |
self.client = None | |
class SysMonWebSocketClient(WebSocketClient): | |
def opened(self): | |
print "opened" | |
def received_message(self, message): | |
print message.data | |
def closed(self, code, reason=None): | |
print code, reason | |
if __name__ == "__main__": | |
sysmon = SysMon(host="ws://127.0.0.1:9000/ws") | |
try: | |
sysmon.run() | |
except KeyboardInterrupt: | |
sysmon.terminate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment