Last active
December 4, 2015 08:28
-
-
Save liuxinglanyue/b165b1dc14e4be17b7ed to your computer and use it in GitHub Desktop.
通过python连接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
# 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