""" pip install ws4py """
import json
from ws4py.client.threadedclient import WebSocketClient class CG_Client(WebSocketClient): def opened(self):
req = '{"event":"subscribe", "channel":"eth_usdt.deep"}'
self.send(req) def closed(self, code, reason=None):
print("Closed down:", code, reason) def received_message(self, resp):
resp = json.loads(str(resp))
data = resp['data']
if type(data) is dict:
ask = data['asks'][0]
print('Ask:', ask)
bid = data['bids'][0]
print('Bid:', bid) if __name__ == '__main__':
ws = None
try:
ws = CG_Client('wss://i.cg.net/wi/ws')
ws.connect()
ws.run_forever()
except KeyboardInterrupt:
ws.close()

  下面介绍另一个比较通用的方法实现websocket:

requirements.txt

websocket_client==0.56.0
six==1.12.0

pip install --no-index --ignore-installed  --find-links=./dependency   -r   requirements.txt
pip download -d ./dependency -r requirements.txt
# coding=utf-8
import ssl
url="localhost:8016/official-website-backend/websocket?"
import wave,os
p="2019-05-06_11-42-06_8611.wav"
path=os.path.join(os.getcwd(),p)
files=wave.open(path,'rb')
data=files.readframes(1024)
print("data type is %s"%type(data))
import json
import websocket def on_message(ws, message): # 服务器有数据更新时,主动推送过来的数据
print(message) def on_error(ws, error): # 程序报错时,就会触发on_error事件
print(error) def on_close(ws):
print("Connection closed ……") def on_open(ws): # 连接到服务器之后就会触发on_open事件,这里用于send数据
req=data
print("发送文件type: %s"%type(data))
ws.send(req) if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp(url,
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.on_open = on_open
ws.on_message=on_message
ws.on_error=on_error
ws.on_close=on_close
ws.run_forever(ping_timeout=30)

最新文章

  1. 【SDOI2010题集整合】BZOJ1922~1927&1941&1951&1952&1972&1974&1975
  2. 光耦PC817
  3. linux lnmp编译安装
  4. iOS开发——开发技巧&Mac常用命令
  5. java.lang.ClassFormatError: Illegal UTF8 string in constant pool in class file Server/Request
  6. Struct标签
  7. linux 监控系统缓存和cpu
  8. (大数据工程师学习路径)第四步 SQL基础课程----修改和删除
  9. Cubieboard Linaro 搭建超节能监控平台
  10. Flutter之List
  11. JavaScript数据类型 String字符串类型的属性和方法
  12. vue单文件组件实例2:简单单文件组件
  13. Android为TV端助力 转载:内存泄露与内存溢出的区别
  14. 360极速浏览器Onetab插件存储位置
  15. apk重签名方法
  16. SSM_CRUD新手练习(2)配置文件
  17. yearProgress.vue
  18. three.js:Failed to execute 'texImage2D' on 'WebGLRenderingContext解决方案
  19. Phalcon的MVC框架解析
  20. C++ list容器系列功能函数详解

热门文章

  1. Vue:模板&渲染函数学习
  2. Code Signal_练习题_differentSymbolsNaive
  3. OSGI企业应用开发(十二)OSGI Web应用开发(一)
  4. Nginx的rewrite(地址重定向)剖析
  5. java持有对象【1】容器类及ArrayList
  6. UWP开发细节记录:IStream 和 IRandomAccessStream^ 以及 IMFByteStream 互转
  7. Python笔记(六):推导数据
  8. [SQLSERVER] [RESTORE] 逐步恢复日志备份并使用只读模式查看
  9. 什么是套接字(Socket)
  10. python爬虫_入门_翻页