Skip to content

Instantly share code, notes, and snippets.

@liuxinglanyue
Last active December 4, 2015 08:28
Show Gist options
  • Save liuxinglanyue/b165b1dc14e4be17b7ed to your computer and use it in GitHub Desktop.
Save liuxinglanyue/b165b1dc14e4be17b7ed to your computer and use it in GitHub Desktop.
通过python连接websocket
# install ws4py
# pip install ws4py
# easy_install ws4py
from ws4py.client.threadedclient import WebSocketClient
class DummyClient(WebSocketClient):
def opened(self):
self.send("www.baidu.com")
def closed(self, code, reason=None):
print "Closed down", code, reason
def received_message(self, m):
print m
if __name__ == '__main__':
try:
ws = DummyClient('ws://10.222.138.163:1889/websocket', protocols=['chat'])
ws.connect()
ws.run_forever()
except KeyboardInterrupt:
ws.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment