Skip to content

Instantly share code, notes, and snippets.

@alphaolomi
Last active January 24, 2024 05:32
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alphaolomi/51f27912abe699dd5db95cfbc21d1a2d to your computer and use it in GitHub Desktop.
Save alphaolomi/51f27912abe699dd5db95cfbc21d1a2d to your computer and use it in GitHub Desktop.
Quote of the Day (QOTD) Protocol

Quote of the Day

from https://www.gkbrk.com/wiki/qotd_protocol/

Tags: networking 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.

The RFC recommends that;

  • The quotes should be limited to the ASCII printable characters, spaces and newlines.
  • They should be less than 512 characters.

Since the protocol is only used to send quotes to terminals, these aren’t hard requirements. But a server should still follow these recommendations in order to be compatible with the other servers and clients.

Despite the name being Quote of the Day, a server does not have to serve daily quotes. It can change the quote at any interval or send random quotes at each connection.

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

Listen to connections on port 17.

  • Accept a connection.
  • Choose a random quote.
  • Send the quote to the client.
  • Close the connection.

UDP Connections

A UDP server is a bit different from a TCP one. Since UDP has no concept of connections, a server just sends the quote after getting a UDP datagram.

Listen to UDP datagrams on port 17.

  • When you get a UDP packet:
  • Discard any data in the packet.
    • Send a UDP datagram containing the quote back to the client.
    • Public QOTD Servers
Server Address TCP Port UDP Port
djxmmx.net 17 17
cygnus-x.net 17 17
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".encode("utf-8"))
sock.close()
@jamiekt
Copy link

jamiekt commented Aug 9, 2023

I discovered (thanks to someone on SO: https://stackoverflow.com/questions/76831582/how-can-i-send-a-request-to-port-17-using-curl?noredirect=1#comment135456932_76831582) then when using python3 the code above throws error:

TypeError: a bytes-like object is required, not 'str'

The fix is simple. Modify:

sock.send(f"{quote}\n")

to be

sock.send(f"{quote}\n".encode("utf-8"))

A request can be sent using netcat:

nc 127.0.0.1 17

image

@alphaolomi
Copy link
Author

Thank you @jamiekt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment