Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python2中构建WAMP协议客户机以实现subscibe?_Python_Subscribe_Wamp Protocol_Poloniex - Fatal编程技术网

如何在Python2中构建WAMP协议客户机以实现subscibe?

如何在Python2中构建WAMP协议客户机以实现subscibe?,python,subscribe,wamp-protocol,poloniex,Python,Subscribe,Wamp Protocol,Poloniex,我想构建一个WAMP(Web应用程序消息传递协议)客户端来订阅poloniex的ticker。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 pro

我想构建一个WAMP(Web应用程序消息传递协议)客户端来订阅poloniex的ticker。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". 
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
但是他们没有说如何使用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()
import websocket
import thread
import time
import json

def on_message(ws, message):
    print(message)

def on_error(ws, error):
    print(error)

def on_close(ws):
    print("### closed ###")

def on_open(ws):
    print("ONOPEN")
    def run(*args):
        ws.send(json.dumps({'command':'subscribe','channel':1001}))
        ws.send(json.dumps({'command':'subscribe','channel':1002}))
        ws.send(json.dumps({'command':'subscribe','channel':1003}))
        ws.send(json.dumps({'command':'subscribe','channel':'BTC_XMR'}))
        while True:
            time.sleep(1)
        ws.close()
        print("thread terminating...")
    thread.start_new_thread(run, ())


if __name__ == "__main__":
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp("wss://api2.poloniex.com/",
                              on_message = on_message,
                              on_error = on_error,
                              on_close = on_close)
    ws.on_open = on_open
    ws.run_forever()
但它显示以下错误:

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". 
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
回溯(最近一次呼叫最后一次):
文件“wamp.py”,第5行,在
从autobahn.asyncio.wamp导入应用程序会话
文件“/usr/lib/python2.7/site packages/autobahn/asyncio/_init__.py”,第36行,在
从autobahn.asyncio.websocket导入\
文件“/usr/lib/python2.7/site packages/autobahn/asyncio/websocket.py”,第32行,在
txaio.use_asyncio()
文件“/usr/lib/python2.7/site-packages/txaio/_-init__uu.py”,第122行,正在使用中
从txaio导入aio
文件“/usr/lib/python2.7/site packages/txaio/aio.py”,第47行,在
导入异步
文件“/usr/lib/python2.7/site packages/asyncio/_init__.py”,第9行,在
从…起导入选择器
文件“/usr/lib/python2.7/site packages/asyncio/selectors.py”,第39行
“{!r}”.format(fileobj))from None
^
SyntaxError:无效语法

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

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

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()
import websocket
import thread
import time
import json

def on_message(ws, message):
    print(message)

def on_error(ws, error):
    print(error)

def on_close(ws):
    print("### closed ###")

def on_open(ws):
    print("ONOPEN")
    def run(*args):
        ws.send(json.dumps({'command':'subscribe','channel':1001}))
        ws.send(json.dumps({'command':'subscribe','channel':1002}))
        ws.send(json.dumps({'command':'subscribe','channel':1003}))
        ws.send(json.dumps({'command':'subscribe','channel':'BTC_XMR'}))
        while True:
            time.sleep(1)
        ws.close()
        print("thread terminating...")
    thread.start_new_thread(run, ())


if __name__ == "__main__":
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp("wss://api2.poloniex.com/",
                              on_message = on_message,
                              on_error = on_error,
                              on_close = on_close)
    ws.on_open = on_open
    ws.run_forever()

感谢作者谁命名瑞奇韩

搜索web套接字订阅。我偶然发现了这个包裹。您可能正在使用poloniex搜索某些过于具体的内容。@按“其他”我不确定websocket客户端和WAMP之间的区别。这和我从WAMP服务器订阅时一样吗。如果你想成为一名认真的开发者,你需要提高你的谷歌搜索技能!