2017-08-16 56 views
0

我想構建一個WAMP(Web應用程序消息傳遞協議)客戶端來訂閱poloniex的代碼。有poloniex的API文檔中的一些常用資料類似以下內容:我如何構建一個WAMP協議客戶端以在Python2中進行子類化?

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來訂閱it.Then我嘗試在谷歌搜索,我coundn't任何幫助。

任何人都可以告訴我如何建立一個WAMP客戶端,以subon的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中正常工作。多麼失望!

+0

搜索網絡套接字訂閱。我遇到了這個包 - https://pypi.python.org/pypi/websocket-client。你可能會搜索一些與poloniex有關的東西。 – PressingOnAlways

+0

@PressingOnAlways我不確定websocket-client和WAMP的區別。當我用WAMP服務器訂閱時是否一樣? –

+0

閱讀http://wamp-proto.org/。 https://github.com/crossbario/autobahn-python。 http://wamp-proto.org/implementations/ https://github.com/noisyboiler/wampy。如果你想成爲一名認真的開發人員,你需要提高你的谷歌搜索技能! – PressingOnAlways

回答

1

最後我發現下面的答案,它可以做我想做的:

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() 

感謝作家誰命名瑞奇涵!

相關問題