How to connect to poloniex.com websocket api using a python library

前端 未结 4 2140
被撕碎了的回忆
被撕碎了的回忆 2020-12-07 17:16

I am trying to connect to wss://api.poloniex.com and subscribe to ticker. I can\'t find any working example in python. I have tried to use autobahn/twisted and websocket-cli

4条回答
  •  醉酒成梦
    2020-12-07 17:49

    What you are trying to accomplish can be done by using WAMP, specifically by using the WAMP modules of the autobahn library (that you are already trying to use).

    After following their docs, I managed to set-up a simple example using autobahn and asyncio. The following example subscribes to the 'ticker' feed and prints the received values:

    from autobahn.asyncio.wamp import ApplicationSession
    from autobahn.asyncio.wamp import ApplicationRunner
    from asyncio import coroutine
    
    
    class PoloniexComponent(ApplicationSession):
        def onConnect(self):
            self.join(self.config.realm)
    
        @coroutine
        def onJoin(self, details):
            def onTicker(*args):
                print("Ticker event received:", args)
    
            try:
                yield from self.subscribe(onTicker, 'ticker')
            except Exception as e:
                print("Could not subscribe to topic:", e)
    
    
    def main():
        runner = ApplicationRunner("wss://api.poloniex.com:443", "realm1")
        runner.run(PoloniexComponent)
    
    
    if __name__ == "__main__":
        main()
    

    You can find more details about WAMP programming with autobahn here: http://autobahn.ws/python/wamp/programming.html

提交回复
热议问题