Websocket error: "Handhshake status 403 forbidden"

  python-3.x, websocket

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:UsersiamdabestDownloadslatexNpythonWinPython64-3.6.8.0python-

3.6.8.amd64libsite-packagesspyder_kernelscustomizespydercustomize.py", line 

786, in runfile
    execfile(filename, namespace)

  File "C:Usersorami?DownloadslatexNpythonWinPython64-3.6.8.0python-

3.6.8.amd64libsite-packagesspyder_kernelscustomizespydercustomize.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:UsersnameDownloadslatexNpythonWinPython64-3.6.8.0python-

3.6.8.amd64libsite-packageswebsocket_core.py", line 606, in 

create_connection
    websock.connect(url, **options)

  File "C:UsersnameDownloadslatexNpythonWinPython64-3.6.8.0python-

3.6.8.amd64libsite-packageswebsocket_core.py", line 253, in connect
    self.handshake_response = handshake(self.sock, *addrs, **options)

  File "C:UsersnameDownloadslatexNpythonWinPython64-3.6.8.0python-

3.6.8.amd64libsite-packageswebsocket_handshake.py", line 57, in handshake
    status, resp = _get_resp_headers(sock)

  File "C:UsersnameDownloadslatexNpythonWinPython64-3.6.8.0python-

3.6.8.amd64libsite-packageswebsocket_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.

Source: Python-3x Questions

LEAVE A COMMENT