gpt4 book ai didi

连接到 cryptocompare 的 Python socketio 示例

转载 作者:太空狗 更新时间:2023-10-30 00:05:04 26 4
gpt4 key购买 nike

我正在尝试使用来自 Python 客户端的 socketIO 连接到 Cryptocompare 的 websocket 流。这被证明是具有挑战性的。下面是一些示例 Python 代码:

from socketIO_client import SocketIO

print "connecting to server"
socketIO = SocketIO('https://streamer.cryptocompare.com/',443, transports=['websocket'])
print "Connected"

但是,无论我做什么,我都无法连接。其实这是那个connect的结果

connecting to server
Traceback (most recent call last):
File "test.py", line 4, in <module>
socketIO = SocketIO('https://streamer.cryptocompare.com/',443, transports=['websocket'])
File "/usr/local/lib/python2.7/site-packages/socketIO_client/__init__.py", line 353, in __init__
resource, hurry_interval_in_seconds, **kw)
File "/usr/local/lib/python2.7/site-packages/socketIO_client/__init__.py", line 54, in __init__
self._transport
File "/usr/local/lib/python2.7/site-packages/socketIO_client/__init__.py", line 62, in _transport
self._engineIO_session = self._get_engineIO_session()
File "/usr/local/lib/python2.7/site-packages/socketIO_client/__init__.py", line 76, in _get_engineIO_session
transport.recv_packet())
StopIteration

将上述代码包装在 try-catch 中并打印异常不会产生额外信息。任何帮助将不胜感激。

最佳答案

socketIO_client 库似乎不支持 cryptocompare 使用的 XHR 轮询协议(protocol)。我通过覆盖 socketIO_client.transports.XHR_PollingTransport 类中的方法 recv_packet 使其工作。

import logging
import socketIO_client
from socketIO_client.transports import get_response
from socketIO_client.parsers import get_byte, _read_packet_text, parse_packet_text

from requests.exceptions import ConnectionError

# extra function to support XHR1 style protocol
def _new_read_packet_length(content, content_index):
packet_length_string = ''
while get_byte(content, content_index) != ord(':'):
byte = get_byte(content, content_index)
packet_length_string += chr(byte)
content_index += 1
content_index += 1
return content_index, int(packet_length_string)

def new_decode_engineIO_content(content):
content_index = 0
content_length = len(content)
while content_index < content_length:
try:
content_index, packet_length = _new_read_packet_length(
content, content_index)
except IndexError:
break
content_index, packet_text = _read_packet_text(
content, content_index, packet_length)
engineIO_packet_type, engineIO_packet_data = parse_packet_text(
packet_text)
yield engineIO_packet_type, engineIO_packet_data

def new_recv_packet(self):
params = dict(self._params)
params['t'] = self._get_timestamp()
response = get_response(
self.http_session.get,
self._http_url,
params=params,
**self._kw_get)
for engineIO_packet in new_decode_engineIO_content(response.content):
engineIO_packet_type, engineIO_packet_data = engineIO_packet
yield engineIO_packet_type, engineIO_packet_data

setattr(socketIO_client.transports.XHR_PollingTransport, 'recv_packet', new_recv_packet)

logging.basicConfig(level=logging.DEBUG)

try:
socket = socketIO_client.SocketIO('https://streamer.cryptocompare.com')
socket.emit('SubAdd', { 'subs': ['0~Kraken~BTC~USD'] });
socket.wait()
except ConnectionError:
print('The server is down. Try again later.')

该解决方案主要基于此 github 评论:https://github.com/invisibleroads/socketIO-client/issues/129#issuecomment-330058318

关于连接到 cryptocompare 的 Python socketio 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46569944/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com