Web servers let you control devices like lights or sensors from anywhere using the internet. This makes managing your gadgets easy and flexible.
Why web servers enable remote IoT control in Raspberry Pi
from http.server import BaseHTTPRequestHandler, HTTPServer class SimpleHandler(BaseHTTPRequestHandler): def do_GET(self): if self.path == '/turn_on': # Code to turn on device self.send_response(200) self.end_headers() self.wfile.write(b'Device turned on') else: self.send_response(404) self.end_headers() server = HTTPServer(('0.0.0.0', 8080), SimpleHandler) server.serve_forever()
This example shows a simple web server on Raspberry Pi that listens for commands.
When you visit /turn_on in a browser, it runs code to control a device.
/status.from http.server import BaseHTTPRequestHandler, HTTPServer class Handler(BaseHTTPRequestHandler): def do_GET(self): if self.path == '/status': self.send_response(200) self.end_headers() self.wfile.write(b'Device is off') server = HTTPServer(('0.0.0.0', 8000), Handler) server.serve_forever()
/toggle is accessed.from http.server import BaseHTTPRequestHandler, HTTPServer class Handler(BaseHTTPRequestHandler): def do_GET(self): if self.path == '/toggle': # Toggle device state here self.send_response(200) self.end_headers() self.wfile.write(b'Device toggled') server = HTTPServer(('0.0.0.0', 8080), Handler) server.serve_forever()
This program runs a web server on Raspberry Pi that lets you turn a device on or off and check its status by visiting URLs.
Try visiting http://your-pi-ip:8080/turn_on to turn it on, /turn_off to turn it off, and /status to see the current state.
from http.server import BaseHTTPRequestHandler, HTTPServer class IoTHandler(BaseHTTPRequestHandler): device_on = False def do_GET(self): if self.path == '/turn_on': IoTHandler.device_on = True self.send_response(200) self.end_headers() self.wfile.write(b'Device turned on') elif self.path == '/turn_off': IoTHandler.device_on = False self.send_response(200) self.end_headers() self.wfile.write(b'Device turned off') elif self.path == '/status': self.send_response(200) self.end_headers() status = 'on' if IoTHandler.device_on else 'off' self.wfile.write(f'Device is {status}'.encode()) else: self.send_response(404) self.end_headers() server = HTTPServer(('0.0.0.0', 8080), IoTHandler) print('Starting server on port 8080...') server.serve_forever()
Make sure your Raspberry Pi is connected to the internet and the port (8080) is open.
Use simple URLs to send commands from any device with a browser.
For real devices, replace comments with actual control code.
Web servers let you control IoT devices remotely using simple web addresses.
This makes managing devices easy from anywhere with internet access.
Raspberry Pi can run these servers to connect your devices to the web.
Practice
Web servers provide a way to access devices over the internet.Solution
Step 1: Understand the role of web servers in IoT
Web servers host web pages and APIs that users can access remotely via the internet.Step 2: Connect remote access to device control
By accessing the web server on the Raspberry Pi, users can send commands to control IoT devices from anywhere.Final Answer:
They allow users to send commands to devices from anywhere using a web browser. -> Option DQuick Check:
Remote control = Web server access [OK]
- Thinking web servers upgrade hardware
- Believing data is stored only locally
- Assuming web servers block internet access
import http.server
import socketserver
PORT = 8000
Handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", PORT), Handler) as httpd:
httpd._____()
Solution
Step 1: Identify the method to keep the server running
The methodserve_forever()keeps the server active and listening for requests.Step 2: Match the method name in the code
The code useshttpd._____(), so the correct method to fill isserve_forever.Final Answer:
serve_forever -> Option AQuick Check:
Keep server running = serve_forever() [OK]
- Using run() which does not exist
- Using start_server() which is not a method
- Using listen() which is for sockets, not HTTPServer
from http.server import SimpleHTTPRequestHandler
from socketserver import TCPServer
PORT = 8080
with TCPServer(("", PORT), SimpleHTTPRequestHandler) as httpd:
print(f"Serving at port {PORT}")
httpd.handle_request()
and a user visits
http://raspberrypi.local:8080/?Solution
Step 1: Understand the use of handle_request()
The methodhandle_request()processes only one HTTP request and then stops.Step 2: Analyze the code behavior on user access
When the user visits the URL, the server will serve that single request and then stop running.Final Answer:
Server handles one request and then stops -> Option AQuick Check:
handle_request() = single request served [OK]
- Confusing handle_request() with serve_forever()
- Expecting server to run continuously
- Thinking print output is the server response
import http.server
import socketserver
PORT = 8000
Handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("localhost", PORT), Handler) as httpd:
httpd.serve_forever
Solution
Step 1: Check method call syntax
The methodserve_forevermust be called with parentheses()to execute.Step 2: Identify the missing parentheses
The code useshttpd.serve_foreverwithout(), so the server will not start.Final Answer:
Missing parentheses after serve_forever method call -> Option BQuick Check:
Method call needs () = Missing parentheses [OK]
- Forgetting parentheses on method calls
- Thinking port number limits cause errors here
- Confusing class and function usage
# Pseudocode Start web server When user visits URL: Receive command (e.g., turn LED on/off) Execute GPIO control based on command Send response back to user
Solution
Step 1: Understand web server role in IoT control
The web server listens for commands sent via internet requests from users.Step 2: Connect commands to hardware control
When a command is received, the server runs code to control the GPIO pins, turning the LED on or off.Final Answer:
The web server listens for internet requests and triggers hardware actions accordingly. -> Option CQuick Check:
Web server + commands = hardware control [OK]
- Thinking server powers hardware automatically
- Believing server only stores data without control
- Assuming server works offline for remote control
Ready to prove it?
Pass the test and this pattern moves unknown
