Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop Iteration error for Socket.io #215

Open
liamhill32 opened this issue Dec 27, 2020 · 6 comments
Open

Stop Iteration error for Socket.io #215

liamhill32 opened this issue Dec 27, 2020 · 6 comments

Comments

@liamhill32
Copy link

liamhill32 commented Dec 27, 2020

Hi,
Im new to coding and im confused how to get around this issue on python.

my code:
def on_connect():
print('Websocket Connected: '+ socketIO._engineIO_session.id)

def on_close():
print('Websocket Closed.')

socketIO = SocketIO(TRADING_API_URL, WEBSOCKET_PORT, params={'access_token': ACCESS_TOKEN})

socketIO.on('connect', on_connect)
socketIO.on('disconnect', on_close)

bearer_access_token = "Bearer " + socketIO._engineIO_session.id + ACCESS_TOKEN

print(bearer_access_token)

--------This gives me a stop iteration error as such ------------------

error:
StopIteration Traceback (most recent call last)
in
5 print('Websocket Closed.')
6
----> 7 socketIO = SocketIO(TRADING_API_URL, WEBSOCKET_PORT, params={'access_token': ACCESS_TOKEN})
8
9 socketIO.on('connect', on_connect)

~/.local/lib/python3.7/site-packages/socketIO_client/init.py in init(self, host, port, Namespace, wait_for_connection, transports, resource, hurry_interval_in_seconds, **kw)
351 super(SocketIO, self).init(
352 host, port, Namespace, wait_for_connection, transports,
--> 353 resource, hurry_interval_in_seconds, **kw)
354
355 # Connect

~/.local/lib/python3.7/site-packages/socketIO_client/init.py in init(self, host, port, Namespace, wait_for_connection, transports, resource, hurry_interval_in_seconds, **kw)
52 if Namespace:
53 self.define(Namespace)
---> 54 self._transport
55
56 # Connect

~/.local/lib/python3.7/site-packages/socketIO_client/init.py in _transport(self)
60 if self._opened:
61 return self._transport_instance
---> 62 self._engineIO_session = self._get_engineIO_session()
63 self._negotiate_transport()
64 self._connect_namespaces()

~/.local/lib/python3.7/site-packages/socketIO_client/init.py in _get_engineIO_session(self)
74 try:
75 engineIO_packet_type, engineIO_packet_data = next(
---> 76 transport.recv_packet())
77 break
78 except (TimeoutError, ConnectionError) as e:

StopIteration:

@amirashoori7
Copy link

I'm working with the same code trying to connect to FXCM Rest api and getting exactly the same error??

any ideas?

@NorberMV
Copy link

Working with socket.io 3.0 server, and getting the same error.

@hugomejia
Copy link

same here ... @amirashoori7 , @NorberMV have you guys found a solution for this issue ? I've seen that even in the official documentation they've changed the socket library (socketIO_client) for the standard SocketIO one in python, but this one has for me a steeper learning curve.

@sanmiawi
Copy link

sanmiawi commented Mar 27, 2022

Hi everyone. It seems the socketIO_client library does not support the XHR polling protocol. I used the following codeblock to get the history.py working again:

import sys
import requests
import socketIO_client
from socketIO_client import SocketIO
from socketIO_client.transports import get_response
from socketIO_client.parsers import get_byte, _read_packet_text, parse_packet_text
import csv

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

if __name__ == '__main__':
    TRADING_API_URL = 'https://api-demo.fxcm.com:443'
    WEBSOCKET_PORT = 443
    socketIO = None
    ACCESS_TOKEN = "YOUR ACCESS TOKEN"

    socketIO = SocketIO(TRADING_API_URL, WEBSOCKET_PORT, params={'access_token': ACCESS_TOKEN})
    bearer_access_token = "Bearer {0}{1}".format(socketIO._engineIO_session.id,ACCESS_TOKEN)
    headers = { 'User-Agent': 'request',
                'Accept': 'application/json',
                'Content-Type': 'application/x-www-form-urlencoded',
                'Authorization' : bearer_access_token}

    instrument_file = "eurusd.csv"
    instrument_id = "1"
    period_id = "m1"
    num = 4000

    f = csv.writer(open(instrument_file, "w+"))
    f.writerows(requests.get('https://api-demo.fxcm.com:443/candles/'+instrument_id+'/'+period_id,
                             headers=headers, params={'num':num}).json()['candles'])

adapted from here

@kylejosephinvestments
Copy link

kylejosephinvestments commented Oct 11, 2022

how do I implement this code?

@Harveyhase68
Copy link

how do I implement this code?

Hi, I'm getting this error too, but I think the fxcmpy rest api is no longer updated... so I move to the .NET component... but I like the python approach

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants