Question

In: Computer Science

To be written in Python: Connect to a TCP-based Quote of the Day server (see RFC...

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.

Solutions

Expert Solution

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

  • Listen to connections on port 17.
  • Accept a connection.
  • Choose a random quote.
  • Send the quote to the client.
  • Close the connection

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.


Related Solutions

How to create a FTP server using python and TCP Ports How to create a FTP...
How to create a FTP server using python and TCP Ports How to create a FTP server using python and UDP Ports
write a skeleton Python TCP servers as follows: Multi-threaded concurrent TCP server using Python’s low-level socket...
write a skeleton Python TCP servers as follows: Multi-threaded concurrent TCP server using Python’s low-level socket module and Python’s threading library Python sockets API: s = socket(), s.bind(), s.listen(), remote_socket, remote_addr = s.accept() Python threading API: thread = threading.Thread(target, args, daemon), thread.start()
How do I make a simple TCP python web client and web server using only "import...
How do I make a simple TCP python web client and web server using only "import socket"? Basically, the client connects to the server, and sends a HTTP GET request for a specific file (like a text file, HTML page, jpeg, png etc), the server checks for the file and sends a copy of the data to the client along with the response headers (like 404 if not found, or 200 if okay etc). The process would be: You first...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT