基本原理
1找都、實(shí)時(shí)數(shù)據(jù)
實(shí)時(shí)數(shù)據(jù)
輪詢
WebSocket
拉模式 由客戶端主動(dòng)從服務(wù)端拉取數(shù)據(jù)
推模式 由服務(wù)端主動(dòng)將數(shù)據(jù)推送給客戶端
aiowebsocket github:https://github.com/asyncins/aiowebsocket
2、安裝:
pip install aiowebsocket
1
實(shí)例
抓取萊特幣官網(wǎng)實(shí)時(shí)數(shù)據(jù) http://www.laiteb.com/
刷新頁面觀察請(qǐng)求
WebSocket 地址為:wss://api.bbxapp.vip/v1/ifcontract/realTime
1赖晶、數(shù)據(jù)交互模式為:
2刀疙、代碼實(shí)例
# -*- coding: utf-8 -*-
import asyncio
from aiowebsocket.converses import AioWebSocket
async def startup(uri):
async with AioWebSocket(uri) as aws:
converse = aws.manipulator
# 給服務(wù)端發(fā)送驗(yàn)證消息囤捻,觀察網(wǎng)頁接口數(shù)據(jù)動(dòng)態(tài)獲取
message = '{"action":"subscribe","args":["QuoteBin5m:14"]}'
await converse.send(message)
while True:
receive = await converse.receive()
# 拿到的是byte類型數(shù)據(jù)食听,解碼為字符串?dāng)?shù)據(jù)
print(receive.decode())
if __name__ == '__main__':
remote = 'wss://api.bbxapp.vip/v1/ifcontract/realTime'
asyncio.get_event_loop().run_until_complete(startup(remote))
3、數(shù)據(jù)效果