0

i have a problem with my written code which i wrote with the help of this documentation: https://exchange.blockchain.com/api/#introduction.

The code should send messages, receive messages and then i want to work with the received messages. And that 3 times per day (in the morning, noon and evening). However the code seems to be working sometimes, actually up to 6 days (record, yay!) and then it stops working and i get an error output:

Traceback (most recent call last):

  File "<ipython-input-1-052db9d33d34>", line 1, in <module>
    runfile('Pathtofile/File.py', wdir='workingdirectory')

  File "C:\Users\iamdabest\Downloads\latexNpython\WinPython64-3.6.8.0\python-

3.6.8.amd64\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 

786, in runfile
    execfile(filename, namespace)

  File "C:\Users\orami?\Downloads\latexNpython\WinPython64-3.6.8.0\python-

3.6.8.amd64\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 

110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "Pathtofile/File.py", line 235, in <module>
    main()

  File "Pathtofile/File.py", line 229, in main
    trigger(updater.bot)

  File "Pathtofile/File.py", line 171, in trigger
    y = price_func()[0]

  File "Pathtofile/File.py", line 46, in price_func
    ws = create_connection(url, **options) 

  File "C:\Users\name\Downloads\latexNpython\WinPython64-3.6.8.0\python-

3.6.8.amd64\lib\site-packages\websocket\_core.py", line 606, in 

create_connection
    websock.connect(url, **options)

  File "C:\Users\name\Downloads\latexNpython\WinPython64-3.6.8.0\python-

3.6.8.amd64\lib\site-packages\websocket\_core.py", line 253, in connect
    self.handshake_response = handshake(self.sock, *addrs, **options)

  File "C:\Users\name\Downloads\latexNpython\WinPython64-3.6.8.0\python-

3.6.8.amd64\lib\site-packages\websocket\_handshake.py", line 57, in handshake
    status, resp = _get_resp_headers(sock)

  File "C:\Users\name\Downloads\latexNpython\WinPython64-3.6.8.0\python-

3.6.8.amd64\lib\site-packages\websocket\_handshake.py", line 143, in 

_get_resp_headers
    raise WebSocketBadStatusException("Handshake status %d %s", status, 

status_message, resp_headers)
 
WebSocketBadStatusException: Handshake status 403 Forbidden

Now please keep in mind that i never got teached this stuff by a trained professional. All the knowledge i gained is from playing around (plotting and analyzing data) with python and the help of the internet. I don't really understand why it is working few days and then suddenly stops working.

My questions:

  1. In fact i have no problem when the codes doesn't gives an output as long as it keeps running. So how can i tell the code to 'ignore' the error and keep moving or maybe to automatic restart the code if the error occurs? I tried simple stuff like if statement or try and expect but i wasn't successful.
  2. The error comes because the authentication between Server and Client failed? or what does it mean?
  3. Is there another workaround? I already read somewhere to use another websocket (websockets) but wasn't able to get it working.

Here is a (M)WE of my code:

from websocket import create_connection
import json

options = {}
options['origin'] = 'https://exchange.blockchain.com'
url = "wss://ws.prod.blockchain.info/mercury-gateway/v1/ws"

def price_func():
        ws = create_connection(url, **options) 
        msg = '{"token": "mysecretAPItoken", "action": "subscribe", "channel": "auth"}' # here i subscribe
        ws.send(msg)
        
        msg = '{"action": "subscribe", "channel": "balances"}' # here i am asking for the json files
        ws.send(msg)
        result1 =  json.loads(ws.recv()) # first answer is the confirmation of connecting
        result2 = json.loads(ws.recv()) # save the received answers to work with them later
        
        msg = '{"token": "mysecretAPItoken", "action": "unsubscribe", "channel": "balances"}' # unsubscribing, not sure if i need to do that but it seems to me like 'clean' working; if smth goes wrong it unsubscribes.
        ws.send(msg) 
        
        msg = '{"token": "mysecretAPItoken", "action": "unsubscribe", "channel": "auth"}' # unsubscribing; same as 3 lines above
        ws.send(msg)
        ws.close() 
        return result2['balances'][0]['rate'], result2['balances'][1]['rate'] # gives me the prices

x = price_func()[0]
print('Price of Shitcoin is around' + str(round(x,2)))

Thx for any help.

Zyska
  • 1
  • 1
  • 2
  • https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/403 – kiner_shah Jan 15 '22 at 09:08
  • Also try using [websocket.enableTrace(True)](https://websocket-client.readthedocs.io/en/latest/examples.html#debug-and-logging-options). – kiner_shah Jan 15 '22 at 09:21
  • Hey, thx for the answer! I implemented the websocket.enableTrace(True) and i will observe it. As soon as i get the error again i will return with the output. – Zyska Jan 15 '22 at 12:38
  • Hi, so i tested the code with websocket.enableTrace(True) and on the 2nd day i had this error `ConnectionResetError: [Errno 104] Connection reset by peer` . does that help? – Zyska Jan 22 '22 at 17:02
  • Issue on server side. If you own the server, check the server logs to see what happens. Related: https://stackoverflow.com/q/20568216 https://stackoverflow.com/q/19383326 https://stackoverflow.com/q/49430073 https://github.com/websocket-client/websocket-client/issues/541 – kiner_shah Jan 23 '22 at 07:23
  • 1
    So it looks like it is working with the `try ... except` solution from that: https://stackoverflow.com/q/20568216 - thanks. I will still keep an eye on it and i will come back if it crashes again. Thanks again for the help. – Zyska Jan 30 '22 at 12:58

0 Answers0