autobahn.twisted.websocket打开握手错误400

时间:2018-02-07 17:10:46

标签: python-3.x api websocket twisted

问题:当Bitfinex websocket API连接建立的应用程序出错时:

2018-02-07T18:51:52+0200 connecting once using transport type "websocket" over endpoint "tcp"
2018-02-07T18:51:52+0200 Starting factory <autobahn.twisted.websocket.WampWebSocketClientFactory object at 0x106708550>
2018-02-07T18:51:52+0200 failing WebSocket opening handshake ('WebSocket connection upgrade failed (400 - BadRequest)')
2018-02-07T18:51:52+0200 dropping connection to peer tcp4:104.16.173.181:443 with abort=True: WebSocket connection upgrade failed (400 - BadRequest)
2018-02-07T18:51:52+0200 component failed: ConnectionAborted: Connection was aborted locally, using.
2018-02-07T18:51:52+0200 Connection failed: ConnectionAborted: Connection was aborted locally, using.
2018-02-07T18:51:52+0200 Stopping factory <autobahn.twisted.websocket.WampWebSocketClientFactory object at 0x106708550>

以下代码:

from autobahn.twisted.component import Component, run
import json

cmp_Bitfinex = Component(
    transports=[
        {
            u'type': u'websocket',
            u'url': u'wss://api.bitfinex.com/ws',
            u'endpoint': {
                u'type': u'tcp',
                u'host': 'api.bitfinex.com',
                u'port': 443,
            },
            u'options': {
                u"open_handshake_timeout": 100,
            }
        }
    ],
    realm=u"realm1",
)

@cmp_Bitfinex.on_join
def joined(self):
    def onTicker(*args):
        print("Ticker event received:", args)

    try:
        yield from self.subscribe(onTicker, 'ticker', options=json.dumps({
            "event": "subscribe",
            "channel": "ticker",
            "pair": "BTCUSD",
            "prec": "P0",
            "freq": "F0"
        }))

    except Exception as e:
        print("Could not subscribe to topic:", e)    

@cmp_Bitfinex.on_connect
def connected(session, details):
    print('Connected: {} \r\n {}'.format(session, details))

def main():
    run([cmp_Bitfinex])    

if __name__ == "__main__":
    main()

据我所知,问题可能出在应用程序发送的数据中。但是,我无法理解我的代码中究竟出了什么问题。

应用程序使用python 3.6,最新高速公路和最新扭曲

1 个答案:

答案 0 :(得分:0)

AFAICT高速公路组件用于WAMP编程。 我认为您应该使用他们的websocket programmingCheck this example