In: Computer Science
To be written in Python:
Connect to a TCP-based Quote of the Day server (see RFC 865) and obtain a random quote and print it to the console.
Solution:
Quote of the Day (QOTD) Protocol
Quote of the Day is a simple protocol that is used to deliver daily quotes. Although its usage is almost nonexistent these days, there are still a few public servers. The protocol is defined by RFC 865. According to the RFC, a QOTD server is run on port 17 for TCP and UDP connections.
TCP Connections
When a QOTD server gets a connection, it sends a quote and closes the connection, discarding any received data. Basically, a server should do the following
Server in Python (Source code):
import socket
import random
quotes = [
"Never do today what you can do tomorrow",
"Nobody lies on the internet",
"The cake is a lie"
]
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("0.0.0.0", 17)) # Bind to port 17
server.listen(5)
while True:
sock, addr = server.accept()
quote = random.choice(quotes)
sock.send(f"{quote}\n")
sock.close()
-------------------------------------------------------------------------------------------------------------------------------------
The client can be anything that connects to a socket and reads data from it.