0
안녕하세요, 저는 websocket에 연결하여 데이터를 가져 오는 autobahnpython을 사용하고 있지만 데이터를 가져 오지 않으려 고합니다. 기본적으로 큐를 사용하여 데이터 자체에서 작동하는 다른 소비자 스레드로 데이터를 보냅니다. 내가 찾은 예제는 수신 된 데이터를 stdout으로 출력하거나 소켓에 보내는 임의의 숫자를 생성합니다.websocket autobahnpython에서 데이터를 가져 오는 방법
import json
from decimal import *
from autobahn.twisted.websocket import WebSocketClientProtocol
import time
class Cryptsy_socket(WebSocketClientProtocol):
_ask_order_queue = []
_bid_order_queue = []
_old_bid_order = {}
_old_ask_order = {}
_bid_order = {}
_ask_order = {}
def set_ask_order_queue(self,queue):
self._ask_order_queue = queue
def set_bid_order_queue(self,queue):
self._bid_order_queue = queue
def onOpen(self):
#subscribe for ltc/btc
self.sendMessage(u"{\"event\": \"pusher:subscribe\",\"data\": {\"channel\": \"ticker.3\"}}".encode("utf8"))
def onConnect(self,response):
print ("Server connectred: {0}".format(response.peer))
def onMessage(self, payload, isBinary):
print("Text message received: {0}".format(payload.decode('utf8')))
json_receive = json.loads(payload.decode('utf8'))
if "event" in json_receive:
if "message" in json_receive["event"]:
json_data = json.loads(json_receive["data"])
buy_order = json_data["trade"]["topbuy"]
sell_order = json_data["trade"]["topsell"]
sell_order_price_as_decimal = Decimal(sell_order["price"])
sell_order_amount_as_decimal = Decimal(sell_order["quantity"])
sell_order_quote_currency_amount = sell_order_price_as_decimal*sell_order_amount_as_decimal
ask_as_decimal = {"price" : sell_order_price_as_decimal,\
"amount": sell_order_amount_as_decimal,\
"amount_of_second_currency": sell_order_quote_currency_amount,\
"name_of_exchange" : "Cryptsy",\
"fee_in_percent" : Decimal("0.25")}
buy_order_price_as_decimal = Decimal(buy_order["price"])
buy_order_amount_as_decimal = Decimal(buy_order["quantity"])
buy_order_quote_currency_amount = buy_order_price_as_decimal*buy_order_amount_as_decimal
bid_as_decimal = {"price" : buy_order_price_as_decimal,\
"amount" : buy_order_amount_as_decimal,\
"amount_of_second_currency" : buy_order_quote_currency_amount,\
"name_of_exchange" : "Cryptsy",\
"fee_in_percent" : Decimal("0.25")}
self._bid_order["order"] = bid_as_decimal
self._ask_order["order"] = ask_as_decimal
if self._old_ask_order == {} or \
self._ask_order["order"]["price"] != self._old_ask_order["order"]["price"] or\
self._ask_order["order"]["amount"] != self._old_ask_order["order"]["amount"]:
ask_order_for_consumer = self._old_ask_order
ask_order_for_consumer["time"] = time.time()
self._ask_order_queue.put(ask_order_for_consumer)
self._old_ask_order = self._ask_order
if self._old_bid_order == {} or \
self._bid_order["order"]["price"] != self._old_bid_order["order"]["price"] or\
self._bid_order["order"]["amount"] != self._old_bid_order["order"]["amount"]:
bid_order_for_consumer = self._old_bid_order
bid_order_for_consumer["time"] = time.time()
self._bid_order_queue.put(bid_order_for_consumer)
self._old_bid_order = self._bid_order
그리고 여기가 큐를 생성하고 연결하는 내 주요이다, 여기가 내 문제를 볼 수 있습니다 여기에 내가
2014-12-28 18:07:04+0100 [-] factory.protocol.set_ask_order_queue(Cryptsy_ask_Queue)
2014-12-28 18:07:04+0100 [-] TypeError: unbound method set_ask_order_queue() must be called
with Cryptsy_socket instance as first argument (got LifoQueue instance instead)
여기 내 WebSocketClientProtocol 파생 클래스
을 받고있어 오류입니다. Cryptsy_socket 생성자가 여기에 호출되지 않습니다. 왜 set_ask_order_queue 및 set_bid_order_queue를 호출하려고 할 때 오류가 발생합니까?import sys
from thread_handling.websocket_process_orders import Cryptsy_socket
from twisted.python import log
from twisted.internet import reactor, ssl
from autobahn.twisted.websocket import WebSocketClientFactory, \
connectWS
import Queue
if __name__ == '__main__':
Cryptsy_ask_Queue = Queue.LifoQueue()
Cryptsy_bid_Queue = Queue.LifoQueue()
log.startLogging(sys.stdout)
factory = WebSocketClientFactory("wss://ws.pusherapp.com:443/app/cb65d0a7a72cd94adf1f?client=PythonPusherClient&version=0.2.0&protocol=6")
factory.protocol = Cryptsy_socket
factory.protocol.set_ask_order_queue(Cryptsy_ask_Queue)
factory.protocol.set_bid_order_queue(Cryptsy_bid_Queue)
## SSL client context: default
##
if factory.isSecure:
contextFactory = ssl.ClientContextFactory()
else:
contextFactory = None
connectWS(factory, contextFactory)
reactor.run()
대기열에서 데이터를 가져 오는 방법은 무엇입니까?