0
0
Raspberry Piprogramming~10 mins

Why web servers enable remote IoT control in Raspberry Pi - Test Your Understanding

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

Complete the code to start a simple web server on the Raspberry Pi.

Raspberry Pi
from http.server import HTTPServer, SimpleHTTPRequestHandler

server = HTTPServer(('0.0.0.0', [1]), SimpleHTTPRequestHandler)
server.serve_forever()
Drag options to blanks, or click blank then click option'
A443
B3306
C22
D8080
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing port 22 which is for SSH, not HTTP.
2fill in blank
medium

Complete the code to handle a GET request and respond with a message.

Raspberry Pi
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]')
Drag options to blanks, or click blank then click option'
AHello, IoT!
BError 404
CUnauthorized
DServer busy
Attempts:
3 left
💡 Hint
Common Mistakes
Using error messages instead of a success message.
3fill in blank
hard

Fix the error in the code to correctly parse the URL path for IoT commands.

Raspberry Pi
from urllib.parse import urlparse

path = urlparse(self.path).[1]
if path == '/led/on':
    self.wfile.write(b'LED turned on')
Drag options to blanks, or click blank then click option'
Aquery
Bpath
Cparams
Dnetloc
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'query' or 'params' instead of 'path'.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps device names to their status if status is 'on'.

Raspberry Pi
devices = {'lamp': 'on', 'fan': 'off', 'heater': 'on'}
on_devices = { [1] : [2] for [1] in devices if devices[[1]] == 'on' }
Drag options to blanks, or click blank then click option'
Adevice
Bdevices[device]
Cstatus
Ddevice_status
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined variable names or wrong dictionary keys.
5fill in blank
hard

Fill all three blanks to create a function that turns on a device and returns a confirmation message.

Raspberry Pi
def control_device([1]):
    devices[[2]] = 'on'
    return f"[3] turned on"
Drag options to blanks, or click blank then click option'
Adevice_name
Dstatus
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causing errors.