Complete the code to start a simple web server on the Raspberry Pi.
from http.server import HTTPServer, SimpleHTTPRequestHandler server = HTTPServer(('0.0.0.0', [1]), SimpleHTTPRequestHandler) server.serve_forever()
The port 8080 is commonly used for web servers on Raspberry Pi for testing and remote control.
Complete the code to handle a GET request and respond with a message.
from http.server import BaseHTTPRequestHandler class Handler(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header('Content-type', 'text/plain') self.end_headers() self.wfile.write(b'[1]')
The server should respond with a friendly message like 'Hello, IoT!' to confirm it is working.
Fix the error in the code to correctly parse the URL path for IoT commands.
from urllib.parse import urlparse path = urlparse(self.path).[1] if path == '/led/on': self.wfile.write(b'LED turned on')
The 'path' attribute of the parsed URL gives the URL path to check commands like '/led/on'.
Fill both blanks to create a dictionary comprehension that maps device names to their status if status is 'on'.
devices = {'lamp': 'on', 'fan': 'off', 'heater': 'on'}
on_devices = { [1] : [2] for [1] in devices if devices[[1]] == 'on' }The comprehension uses 'device' as the key and 'devices[device]' as the value for devices that are 'on'.
Fill all three blanks to create a function that turns on a device and returns a confirmation message.
def control_device([1]): devices[[2]] = 'on' return f"[3] turned on"
The function takes 'device_name' as input, sets its status to 'on', and returns a message using the same variable.