如何构建一个WAMP协议客户端以在Python2中进行subscibe?

时间:2017-08-16 02:36:44

标签: python subscribe wamp-protocol poloniex

我想构建一个WAMP(Web应用程序消息传递协议)客户端来订阅poloniex的代码。 poloniex的API文档中有一些有用的信息,如下所示:

The best way to get public data updates on markets is via the push API,
which pushes live ticker, order book, trade, and Trollbox updates over 
WebSockets using the WAMP protocol. In order to use the push API, 
connect to wss://api.poloniex.com and subscribe to the desired feed. 
In order to receive ticker updates, subscribe to "ticker". 

但他们没有说如何使用python订阅它。然后我尝试在谷歌搜索,我没有任何帮助。

任何人都可以告诉我如何构建一个WAMP客户端来对poloniex的自动收录器进行分类吗?谢谢!

-----------更新-------------- 我发现代码遵循了我想要的东西:

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(u"wss://api.poloniex.com:443", "realm1")
    runner.run(PoloniexComponent)


if __name__ == "__main__":
    main()

但它显示以下错误:

Traceback (most recent call last):
  File "wamp.py", line 5, in <module>
    from autobahn.asyncio.wamp import ApplicationSession
  File "/usr/lib/python2.7/site-packages/autobahn/asyncio/__init__.py", line 36, in <module>
    from autobahn.asyncio.websocket import \
  File "/usr/lib/python2.7/site-packages/autobahn/asyncio/websocket.py", line 32, in <module>
    txaio.use_asyncio()
  File "/usr/lib/python2.7/site-packages/txaio/__init__.py", line 122, in use_asyncio
    from txaio import aio
  File "/usr/lib/python2.7/site-packages/txaio/aio.py", line 47, in <module>
    import asyncio
  File "/usr/lib/python2.7/site-packages/asyncio/__init__.py", line 9, in <module>
    from . import selectors
  File "/usr/lib/python2.7/site-packages/asyncio/selectors.py", line 39
    "{!r}".format(fileobj)) from None
                               ^
SyntaxError: invalid syntax

我发现了一些线索,似乎某些模块只能在python3中正常工作。多么失望!

1 个答案:

答案 0 :(得分:1)

最后我找到了答案,它可以做我想要的:

int(evaluation)

感谢作者Ricky Han!