python stomp订阅websocket
这两天被这个搞的头大,一般的订阅websocket都是没问题的,这个用的是stomp,这下之前订阅的方法就不行了,查了很多资料,都很少有关于这方面的资料;
一直在stomp模块上下功夫,但是这个模块连接的时候要ip和端口,不靠谱,怎么都连不上的,用stomp的代码如下,可以看到connection这里要ip和端口:
import stomp
class MyListener(object):
def on_error(self, headers, message):
print('received an error %s' % message)
def on_message(self, headers, message):
print('received a message %s' % message)
conn = stomp.Connection([('115.28.253.107', 61613)])
conn.set_listener('', MyListener())
conn.start()
conn.connect('geust', 'guest', wait=True)
conn.send(body='SUBSCRIBE', destination='/WALEOS@depth40')
conn.subscribe(destination='/WALEOS@depth40', id='sub-0', ack='auto')
conn.disconnect()
其实也想用node连,脚本都写好了数据也能拿到,奈何对node了解不多,对数据处理都没操作过,还有部署,最终还是放弃了;
最后无意中用google搜了下看到一个人用stomper,而且用的正常的订阅,貌似找到了希望,赶紧操作一波,可以获得数据,搞定,代码如下:
import stomper
import ssl
import random
import time
from websocket import create_connection
url ='wss://w.whaleex.com.cn/ws/websocket'
ws = create_connection(url, sslopt={"cert_reqs": ssl.CERT_NONE})
v =str(random.randint(0, 1000))
sub = stomper.subscribe("/WALEOS@depth10", v, ack='auto')
sub_plus = stomper.send("/WALEOS@depth10", '')
ws.send(sub)
ws.send(sub_plus)
sub1 = stomper.subscribe("/WALEOS@trade", v, ack='auto')
sub2 = stomper.send("/WALEOS@trade", '')
ws.send(sub1)
ws.send(sub2)
l = [sub2, sub_plus]
while True:
for i in l:
content = ws.recv()
if 'symbol' in content:
print('======', content, '\n=======')
continue
print('--->', content, '\n+-------+')
ws.send(i)
time.sleep(1)
网友评论