0

I have a webpage that is running some web sockets.

When I access the webpage with the browser it all works fine.

When I access the webpage with requests, the page doesn't initiate the socket.

How is this possible?

Sockets:

<script>

    $(document).ready(function () {
            const messages_container = document.getElementById('chat_box');
            let room_id = "{{ room.id }}";

            var loc = window.location;
            var wsStart = 'ws://';

            if (loc.protocol == 'https:') {
                var wsStart = 'wss://';
            }

            let chatSocket = new WebSocket(
                wsStart
                + window.location.host
                + '/ws/matchmaker/'
                + room_id
                + '/'
            );

            $('#game_results').click(function () {
                chatSocket.send(JSON.stringify({
                    'message': 'results-incoming'
                }));
            });

            chatSocket.onopen = function (e) {
                // nothing
            };

            chatSocket.onmessage = function (e) {
                let message = JSON.parse(e.data)['message'];

                if (message['message-type'] == 'get-room-players') {
                    let message_body = JSON.parse(message['message-body']);
                    let players = "";
                    for (let i = 0; i < message_body.length; i++) {
                        players += `<div class="col-md-6 card">
                        <div class="card-body">
                            <h5 lass="card-title">player id ${message_body[i].fields['player_id']}</h5>
                            <h5 lass="card-title">player skill ${message_body[i].fields['skill']}</h5>
                            <h5 lass="card-title">player behaviour ${message_body[i].fields['behaviour']}</h5>
                            <h5 lass="card-title">player skill preference ${message_body[i].fields['skill_preference']}</h5>
                            <h5 lass="card-title">player behaviour preference ${message_body[i].fields['behaviour_preference']}</h5>
                            <h5 lass="card-title">player granularity_preference ${message_body[i].fields['granularity_preference']}</h5>
                        </div>
                    </div>`

                    }
                    $('#players_container').html(players);
                } else if (message['message-type'] == 'update-players') {
                    chatSocket.send(JSON.stringify({
                        'message': 'get-room-players'
                    }));
                } else if (message['message-type'] == 'update-room-match-status') {
                    $('#match_status').text(message['message-body']);
                } else if (message['message-type'] == 'results-incoming') {
                    window.location = "http://localhost:8002/game/?room_id=" + room_id;

                } else if (message['message-type'] == 'update-current-num-players') {
                    $('#current_num_players').text(message['message-body']);
                    // room is now full and can proceed to play
                    if (message['message-body'] == {{ room.game.max_num_players }}) {
                        $('#game_results').show();

                    }
                }

            };

            chatSocket.onclose = function (e) {
                console.error('Chat socket closed unexpectedly');
            };
        }
    );


</script>

The webpage which has the sockets is a response to this post request:

response = client.post(url, headers=headers, params=params, data=data, cookies=cookies)

If I print(response.text) the html code for the web socket webpage is shown, but as I said, the socket does not run.

  • 2
    What makes you think merely downloading the data will execute the data? You will need to pass the downloaded `response.text` to an appropriate JavaScript engine to execute that code. Browsers typically do this because scripting is enabled, if it is disabled, much like `requests` it will only download the data and not execute it. – metatoaster Jun 17 '22 at 03:00
  • You probably want [`selenium`](https://selenium-python.readthedocs.io/) or similar, not `requests` for this. Alternately, you can implement your own clientside logic directly in Python using [`websockets`](https://websockets.readthedocs.io/). – Amadan Jun 17 '22 at 03:04

0 Answers0