Bird
0
0

Identify the mistake in this Python WebSocket client code on a Raspberry Pi:

medium📝 Debug Q6 of 15
Raspberry Pi - Web Server and API
Identify the mistake in this Python WebSocket client code on a Raspberry Pi:
import asyncio
import websockets

async def connect():
    ws = await websockets.connect('ws://localhost:5678')
    await ws.send('Hello')
    response = await ws.recv
    print(response)

asyncio.run(connect())
AMissing parentheses on ws.recv call.
BIncorrect import statement for websockets.
CUsing await outside an async function.
DNo event loop started with asyncio.
Step-by-Step Solution
Solution:
  1. Step 1: Check method calls

    The method ws.recv is a coroutine and must be called with parentheses: ws.recv().
  2. Step 2: Verify async usage

    The code correctly uses await inside an async function and runs the event loop with asyncio.run().
  3. Final Answer:

    Missing parentheses on ws.recv call. -> Option A
  4. Quick Check:

    Ensure coroutine methods are called with () [OK]
Quick Trick: Call coroutine methods with parentheses () [OK]
Common Mistakes:
MISTAKES
  • Forgetting parentheses on async method calls
  • Misusing await outside async functions
  • Not running asyncio event loop

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes