I need to receive data from a website over a websocket connection. Access to the websocket is only given after logging into the website. Using a session in python requests I am able to post my login information to the login page and authenticate my details. Websocket-client would be used for the creating the websocket connection to the website however the websocket connection created would not be through the login session.
How can I create a websocket connection after logging in through requests?
I found a similiar question here but it has not been answered Python - Websockets Unable to validate my session
This is my code so far (simplified a bit).
import requests
from bs4 import BeautifulSoup
import base64
import random
import websocket
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'}
payload = {
'username': 'username',
'password': 'password'}
session = requests.Session()
r = session.get("https://www.example.com/login", headers=headers)
soup = BeautifulSoup(r.content, 'html.parser')
payload['token'] = soup.find('input', attrs={'name': 'token'})['value']
r = session.post("https://www.example.com/login", data=payload, headers=headers)
r = session.get("https://www.example.com/", headers=headers)
headers['Sec-WebSocket-Key'] = str(base64.b64encode(bytes([random.randint(0, 255) for _ in range(16)])), 'ascii')
headers['Sec-WebSocket-Version'] = '13'
headers['Upgrade'] = 'websocket'
ws = websocket.WebSocketApp('wss://www.example.com/streaming/',
header=headers,
#session=session????)
ws.run_forever()