I was connecting to a websocket a website to collect data from python module websockets but it stopped working for some unknown reason for now. But I noticed that if I connect through the browser with javascript, regardless of the website I connect to, the data comes to me, whether cookies are disabled or not. I would like to know if there is any way to identify if the connection is through the browser, or some other way, so that I can try to work around this.
successful connecting by javascript in any website:
const ws = new WebSocket("wss://premws-pt1.365lpodds.com/zap", "zap-protocol-v1");
ws.addEventListener('open', (e) => {
console.log("connected")
})
ws.addEventListener('message', (message) => {
console.log(message.data)
})
ws.addEventListener('error', (error) => {
console.log(error)
})
output: connected
unsuccessful connection by python using websockets package:
import asyncio
import websockets
headers = {
'Pragma': 'no-cache',
'Accept-Language': 'pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36',
'Upgrade': 'websocket',
'Sec-WebSocket-Extensions': 'permessage-deflate; client_max_window_bits',
'Cache-Control': 'no-cache',
'Connection': 'Upgrade',
'Sec-WebSocket-Version': '13',
}
async def main():
try:
async with websockets.connect('wss://premws-pt1.365lpodds.com/zap/', extra_headers=headers, origin="https://www.bet365.com", subprotocols=['zap-protocol-v1']) as websocket:
print("connected")
except websockets.exceptions.InvalidStatusCode as e:
print(e)
asyncio.get_event_loop().run_until_complete(main())
output: server rejected WebSocket connection: HTTP 403