python3 写一个简单的websocket程序(转)


原贴:https://segmentfault.com/q/1010000009284816?_ea=1883181

也是找了好久

#! /usr/bin/env python
# -*- coding:utf-8 -*-
# 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("recv:",m)


if __name__ == '__main__':
    try:
        ws = DummyClient('ws://localhost:9002/websocket', protocols=['chat'])
        ws.connect()
        ws.send("my test...")
        ws.run_forever()
    except KeyboardInterrupt:
        ws.close()

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM