coinbasepro-python
coinbasepro-python copied to clipboard
OSError: [Errno 65] No route to host
Hi,
I have the following code that works fine on Mac Terminal.
import json
import time
import sys
import cbpro
import os
from dotenv import load_dotenv
load_dotenv(verbose=True)
"""LOAD UP THE KEYS from .env
"""
API_KEY = os.getenv("CB_PRO_API_KEY")
API_SECRET = os.getenv("CB_PRO_API_Secret")
API_PASS = os.getenv("CB_PRO_API_PASSPHRASE")
API_URL = os.getenv("CB_PRO_API_ENDPOINT")
CB_WEB_SOC_API = os.getenv("CB_WEB_SCO_ENDPOINT")
CB_PRO_API = os.getenv("CB_PRO_API_ENDPOINT")
class MyCBPWebsocketClientNew(cbpro.WebsocketClient):
# over riding the on_message method from the cbpro lib to push the records into mysql
# comment out on_message() below and it will print in normal json format i.e. will call the super method
def on_message(self, msg):
print(json.dumps(msg))
if __name__ == "__main__":
SOCKET_CONFIG = {'products': ['BTC-GBP', 'BTC-USD', 'BTC-EUR', 'ETH-GBP', 'ETH-USD', 'ETH-EUR'],
'auth': True,
'api_key': API_KEY,
'api_secret': API_SECRET,
'api_passphrase': API_PASS,
'channels': ['level2'], # full, ticker, heartbeat, level2
'url': CB_WEB_SOC_API
}
wsClient = MyCBPWebsocketClientNew(**SOCKET_CONFIG)
print(f"Getting data from = {wsClient.url},\n For the following products: {wsClient.products},\n For the following "
f"channels: {wsClient.channels}")
wsClient.start()
try:
while True:
time.sleep(0.1)
except KeyboardInterrupt:
wsClient.close()
if wsClient.error:
sys.exit(1)
else:
sys.exit(0)
But the same code gives the error when I take it out to a FreeBSD terminal
OSError: [Errno 65] No route to host
However, On the FreeBSD. The following code works fine:
import asyncio
import os
from copra.rest import Client
from dotenv import load_dotenv
load_dotenv(verbose=True)
"""LOAD UP THE KEYS from .env
"""
API_KEY = os.getenv("CB_PRO_API_KEY")
API_SECRET = os.getenv("CB_PRO_API_Secret")
PASSPHRASE = os.getenv("CB_PRO_API_PASSPHRASE")
from copra.websocket import Channel, Client
loop = asyncio.get_event_loop()
channel = Channel('heartbeat', 'BTC-GBP')
ws = Client(loop, channel, auth=True, key=API_KEY, secret=API_SECRET, passphrase=PASSPHRASE)
try:
loop.run_forever()
except KeyboardInterrupt:
loop.run_until_complete(ws.close())
But the Following code fails in FreeBSD but again works fine on Mac Terminal.
from websocket import create_connection
import json
ws = create_connection("wss://ws-feed.pro.coinbase.com")
# Request
# Subscribe to ETH-GBP and BTC-GBP with the level2, heartbeat and ticker channels,
# plus receive the ticker entries for ETH-BTC and ETH-USD
Subscribe = {
"type": "subscribe",
"product_ids": [
"ETH-GBP",
"BTC-GBP"
],
"channels": [
"level2",
"heartbeat",
"full",
{
"name": "ticker",
"product_ids": [
"ETH-BTC",
"ETH-USD"
]
}
]
}
print(json.dumps(Subscribe))
ws.send(json.dumps(Subscribe))
while True:
try:
msg = ws.recv()
print(msg)
except KeyboardInterrupt:
ws.close() # close socket
print("Good BYE!")
# ws.close()
Any ideas what's wrong?
Both Mac and the FreeBSD connected to the same router.
Sounds like a networking issue. Are you able to ping other devices from the FreeBSD machine?