0
0
Raspberry Piprogramming~10 mins

WebSocket for live updates in Raspberry Pi - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the WebSocket client library.

Raspberry Pi
import [1]
Drag options to blanks, or click blank then click option'
Arequests
Bsocket
Chttp
Dwebsocket
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'socket' instead of 'websocket' will not provide WebSocket client features.
Using 'requests' is for HTTP, not WebSocket.
2fill in blank
medium

Complete the code to create a WebSocket connection to the server URL.

Raspberry Pi
ws = websocket.WebSocket()
ws.[1]("ws://example.com/live")
Drag options to blanks, or click blank then click option'
Acreate
Bconnect
Copen
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'open' or 'start' will cause an AttributeError.
Using 'create' is not a valid method for WebSocket client.
3fill in blank
hard

Fix the error in the code to receive a message from the WebSocket.

Raspberry Pi
message = ws.[1]()
Drag options to blanks, or click blank then click option'
Arecv
Bsend
Creceive
Dread
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'send' tries to send data instead of receiving.
Using 'receive' or 'read' are not valid methods in this library.
4fill in blank
hard

Fill both blanks to send a JSON message with a type and data.

Raspberry Pi
import json

msg = json.dumps({"type": [1], "data": [2])
ws.send(msg)
Drag options to blanks, or click blank then click option'
A"update"
B"live"
C"sensor"
D"value"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'live' as type is incorrect here.
Using 'sensor' as data key is not expected.
5fill in blank
hard

Fill both blanks to create a dictionary comprehension filtering live data with values above 50.

Raspberry Pi
live_data = {k: v for k, v in data.items() if v {BLANK_1}} {{BLANK_2}}
filtered = {k: v for k, v in live_data.items()}
Drag options to blanks, or click blank then click option'
A>
B50
C:
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of ':' in dictionary comprehension causes syntax error.
Using '<' instead of '>' changes the filter logic.