Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with this python code assignent. I am asked to run a qoute of the day client/server with the following instructions: The client program

Need help with this python code assignent. I am asked to run a qoute of the day client/server with the following instructions:

The client program should:

1. accept a host and port number from the command line

or prompt the user for these values at startup

2. Request the QOTD from the specified host and port over UDP

3. Print out the resulting quote

4. Extra Credit (2 points) (Only accept responses from the specified server)

5. Extra Credit (2 points) (Do not wait forever to receive a response. Time-out after 10 seconds and retry twice. Print a suitable message and exit gracefully)

The server program should:

1. accept a port number from the command line

or prompt the user for this value at startup

2. Receive requests from any address

3. Reply to requests with the QOTD

This can be a single hard-coded quote.

Extra Credit (2 points) rotate through 3 or more quotes

the following is the code I have so far:

//qotdclient2.py

import sys import socket

def getServerDetails(): serverConnectAddress = None serverPort = -1

if len(sys.argv) > 2: serverConnectAddress = sys.argv[1] serverPort = sys.argv[2]

try: serverPort = int(serverPort) except ValueError: serverPort = -1 print("Invalid Port number - Port number must be between 1 and 65535")

elif len(sys.argv) == 2: serverConnectAddress = sys.argv[1]

if serverConnectAddress == None: serverConnectAddress = input("Enter the server IP address: ")

if serverPort == -1: serverPort = int(input("Enter the Server port number (1-65535): "))

return serverConnectAddress, serverPort

def getQuotdFromServer(serverAddress, serverPort):

print ("Connecting to server at - ", serverAddress, serverPort)

clientSocket = None try: clientSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) clientSocket.sendto("".encode(), (serverAddress, serverPort))

#Wait for 10s for server to respond, retry twice recvdResponse = False retryCount = 0 while retryCount < 2: try: clientSocket.settimeout(60.0) print ("Waiting for response from server for 60s ....") serverData, serverAddress = clientSocket.recvfrom(4096)

print ("Server sent the following QUOTD") print ("") print ("==================================================================================================================") print (serverData) print ("==================================================================================================================") recvdResponse = True break

except socket.timeout: print ("No response from server. Timed out. RetryCount #", str(retryCount+1) ) retryCount = retryCount + 1

if recvdResponse == False: print ("There was no response from server. Please check the server address and the port. This client shall terminate now." )

except socket.error as ex: print (ex) finally: if clientSocket != None: clientSocket.close()

def main(): serverAddress, serverPort = getServerDetails() getQuotdFromServer(serverAddress, serverPort)

if __name__ == "__main__": main()

and for server

//qotdserver2.py

import sys import socket import random

SERVER_LISTEN_ADDRESS = "0.0.0.0"

#QUOTD from Forbes - https://www.forbes.com/quotes

QOTD_LIST = ["Teach self-denial and make its practice pleasure, and you can create for the world a destiny more sublime that ever issued from the brain of the wildest dreamer. - Sir Walter Scott", "It is the hardest thing in the world to be a good thinker without being a good self examiner. - Lord Shaftesbury", "Truly, this world can get on without us, if we would but think so. - Henry Wadsworth Longfellow", "Human history is, in essence, a history of ideas. - H.G. Wells", "The price of greatness is responsibility. - Winston Churchill"]

def getListenPort(): if len(sys.argv) > 1: port = sys.argv[1] else: port = input("Enter a port number (1-65535): ")

return port

def startQuotdServer(port): print ("Server listening at ", SERVER_LISTEN_ADDRESS, port) print ("Press (Ctrl+C) to terminate this server")

serverSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) serverSocket.bind((SERVER_LISTEN_ADDRESS, int(port)))

try: while True: clientData, clientAddress = serverSocket.recvfrom(4096) sentBytes = serverSocket.sendto(random.choice(QOTD_LIST), clientAddress)

except KeyboardInterrupt: print ("Terminating program")

except Exception as exc: print ("Error: Unhandled error. Terminating ...", exc)

def main(): port = getListenPort() startQuotdServer(port)

if __name__ == "__main__": main()

when running the server through command prompt i enter the port number 9000

Then I proceed to opening a new command prompt to run the client, then enter the address of which i have tried entering in 'localhost', '127.0.0.1' and/or my ipv4 address which is '192.168.1.13'

once i hit enter i get the following issue:

I used python 3.6.3 if that helps.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Seven NoSQL Databases In A Week Get Up And Running With The Fundamentals And Functionalities Of Seven Of The Most Popular NoSQL Databases

Authors: Aaron Ploetz ,Devram Kandhare ,Sudarshan Kadambi ,Xun Wu

1st Edition

1787288862, 978-1787288867

More Books

Students also viewed these Databases questions