'How do I update the ssl certificate when running websocket connection? (Alpaca API websocket)

When I'm running below code, nothing happens even though the code should print "opened" , then "received a message" and print the message from the websocket, which is 'connected'.

After some investigation I found that the ssl certificate has expired - I got the error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired So I updated the code with sslopt={"cert_reqs": ssl.CERT_NONE} to ignore the validation of the certificate.

This got the connection to work. Now instead the connection is lost shortly after connecting to the websocket url and I would like to understand better what I need to do update my certificate and solve the root issue - and not use workarounds like sslopt={"cert_reqs": ssl.CERT_NONE}

I'm new to stackOverflow so please excuse me if this questions is not well put/wrongly categorized.

As a side-note, connecting to the below websocket url through wscat in the terminal creates no issues at all, so I'm confused.

Thanks in advance for all the help,

import websocket
import ssl
           
def on_open(ws):
    print("opened")
         
def on_message(ws, message):
    print("received a message")
    print(message)
                     
socket = "wss://stream.data.alpaca.markets/v2/iex"
             
ws = websocket.WebSocketApp(socket, on_open=on_open, on_message=on_message)
ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source