之前的对接方式
代码一
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 
 | import asyncio
 import aiohttp
 
 
 async def ws_mexc(loop=None):
 session = aiohttp.ClientSession()
 async with session.ws_connect('wss://wbs.mexc.com/ws', proxy="http://127.0.0.1:8001") as ws:
 await ws.send_str('{"method": "SUBSCRIPTION","params":["spot@public.bookTicker.v3.api@BTCUSDT"]}')
 await loop.create_task(parse(ws))
 
 
 async def parse(ws):
 async for msg in ws:
 if msg.type == aiohttp.WSMsgType.TEXT:
 print(msg)
 await asyncio.sleep(0.0001)
 
 
 if __name__ == '__main__':
 loop = asyncio.get_event_loop()
 loop.create_task(ws_mexc(loop))
 loop.run_forever()
 
 | 
这个用的还是 python3.5 方法,不够先进。
代码二
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 
 | import asyncioimport threading
 import time
 
 import aiohttp
 
 
 async def ws_mexc():
 session = aiohttp.ClientSession()
 async with session.ws_connect('wss://wbs.mexc.com/ws', proxy="http://127.0.0.1:8001") as ws:
 await ws.send_str('{"method": "SUBSCRIPTION","params":["spot@public.bookTicker.v3.api@BTCUSDT"]}')
 loop = asyncio.get_event_loop()
 await loop.create_task(parse(ws))
 
 
 async def parse(ws):
 async for msg in ws:
 if msg.type == aiohttp.WSMsgType.TEXT:
 print(msg)
 await asyncio.sleep(0.0001)
 
 
 async def main():
 task1 = asyncio.create_task(ws_mexc())
 await asyncio.gather(task1)
 
 
 def start_asyncio_loop():
 asyncio.run(main())
 
 
 if __name__ == '__main__':
 start_asyncio_loop()
 
 | 
这个的灵活性不够。
代码三
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 
 | import asyncio
 import aiohttp
 
 
 async def ws_mexc():
 session = aiohttp.ClientSession()
 ws = await session.ws_connect('wss://wbs.mexc.com/ws', proxy="http://127.0.0.1:8001")
 await send(ws)
 loop = asyncio.get_event_loop()
 await loop.create_task(parse(ws))
 
 
 async def send(ws):
 await ws.send_str('{"method": "SUBSCRIPTION","params":["spot@public.bookTicker.v3.api@BTCUSDT"]}')
 
 
 async def parse(ws):
 async for msg in ws:
 if msg.type == aiohttp.WSMsgType.TEXT:
 print(msg)
 await asyncio.sleep(0.0001)
 
 
 async def main():
 task1 = asyncio.create_task(ws_mexc())
 await asyncio.gather(task1)
 
 
 def start_asyncio_loop():
 asyncio.run(main())
 
 
 if __name__ == '__main__':
 start_asyncio_loop()
 
 | 
代码四
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 
 | import asyncio
 import aiohttp
 
 
 class Mexc():
 
 def __init__(self):
 self.ws = None
 
 async def subscribe(self):
 session = aiohttp.ClientSession()
 self.ws = await session.ws_connect('wss://wbs.mexc.com/ws', proxy="http://127.0.0.1:8001")
 await self.send()
 loop = asyncio.get_event_loop()
 await loop.create_task(self.parse())
 
 async def send(self):
 await self.ws.send_str('{"method": "SUBSCRIPTION","params":["spot@public.bookTicker.v3.api@BTCUSDT"]}')
 
 async def parse(self):
 async for msg in self.ws:
 if msg.type == aiohttp.WSMsgType.TEXT:
 print(msg)
 await asyncio.sleep(0.0001)
 
 
 async def main():
 mexc = Mexc()
 task1 = asyncio.create_task(mexc.subscribe())
 await asyncio.gather(task1)
 
 
 def start_asyncio_loop():
 asyncio.run(main())
 
 
 if __name__ == '__main__':
 start_asyncio_loop()
 
 |