Bird
0
0

Given this Python code running on a Raspberry Pi using the 'websockets' library, what will be printed when a client connects and sends 'Hello'?

medium📝 Predict Output Q13 of 15
Raspberry Pi - Web Server and API
Given this Python code running on a Raspberry Pi using the 'websockets' library, what will be printed when a client connects and sends 'Hello'?
import asyncio
import websockets

async def handler(websocket):
    message = await websocket.recv()
    print(f'Received: {message}')

async def main():
    async with websockets.serve(handler, 'localhost', 8765):
        await asyncio.Future()

asyncio.run(main())
AReceived: Hello
BReceived: websocket
CError: No client connected
DReceived: None
Step-by-Step Solution
Solution:
  1. Step 1: Understand the handler function

    The handler waits to receive a message from the client and then prints it with 'Received: ' prefix.
  2. Step 2: Analyze what happens when client sends 'Hello'

    The message 'Hello' is received and printed as 'Received: Hello'.
  3. Final Answer:

    Received: Hello -> Option A
  4. Quick Check:

    Received message = 'Hello' [OK]
Quick Trick: Handler prints whatever client sends prefixed by 'Received:' [OK]
Common Mistakes:
MISTAKES
  • Assuming it prints 'websocket' instead of message
  • Expecting an error without client connection
  • Thinking it prints None if message missing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes