Protocols
Standards, and TCP, UDP, IP, HTTP, HTTPS, FTP, SMTP, POP and IMAP.
Do this lesson in the simulatorTwo devices can only communicate if they agree on the rules: what a message looks like, what order things happen in, and what to do when something goes wrong. Those rules are a protocol. You met the idea in module 9, where robots agreed on messages like forward 30. The internet runs on a set of protocols that every device in the world follows.
Standards
A standard is a set of rules agreed across an industry, so that equipment from different makers works together. A phone made by one company can join a Wi-Fi network built by another, and load a web page from a server built by a third, because all of them follow the same standards.
The protocols on the exam
| Protocol | What it is for |
|---|---|
| TCP (Transmission Control Protocol) | splits data into packets, numbers them, checks they all arrive, puts them back in order and asks for missing ones again |
| UDP (User Datagram Protocol) | sends packets without checking they arrive. Faster, used for live video and games where a late packet is useless anyway |
| IP (Internet Protocol) | addresses packets and routes them between networks |
| HTTP (Hypertext Transfer Protocol) | how a browser requests web pages from a web server, and how the server replies |
| HTTPS (HTTP Secure) | HTTP with the data encrypted, so it cannot be read if intercepted. Used for logins and payments |
| FTP (File Transfer Protocol) | sends files between computers |
| SMTP (Simple Mail Transfer Protocol) | sends email to a mail server, and between mail servers |
| POP (Post Office Protocol) | downloads email from the server to one device, usually removing it from the server |
| IMAP (Internet Message Access Protocol) | reads email kept on the server, so it stays in sync on every device |
| Ethernet | how devices send data over a wired LAN |
| Wi-Fi | how devices send data over a wireless LAN |
TCP and IP work together, so they are often called TCP/IP.
Requests and responses
An HTTP request names a method and a path: GET /index.html asks for a page. The server replies with a status code and the content. 200 means OK; 404 means Not Found. The Hub robot on this mat speaks a tiny version of HTTP:
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
send("GET /battery")
for tick in range(10):
wait(0.1)
for sender, text in messages():
status, _, body = text.partition(" ")
print("status:", status, "| the rest:", body)
Task: a tiny web client
Write request(path), which sends GET <path>, waits up to a second for the reply, and returns the status code as a number and the rest of the reply as text. Request /battery, /camera and /lights. For a status of 200, print <name>: <rest> (for example battery: 87, without the OK); otherwise print <name>: error <status>. Turn the LED green if the battery is above 50, and red if not.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
Challenges
- Add a timeout: if no reply comes, print
<name>: no reply. - What status code would mean "you are not allowed"? Look up 403.
- For each of these, say which protocol is used: uploading a website to a server; reading email on a phone and a laptop; a live video call.