Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import polynomial import socket import logging port=12345 #socket() listener=socket.socket() #bind(): Ip address with port number listener.bind(( '127.0.0.1' ,port)) #listen() listener.listen(0) #accept() while True : (sock,addr)=listener.accept()

import polynomial import socket import logging port=12345 #socket() listener=socket.socket() #bind(): Ip address with port number listener.bind(('127.0.0.1',port)) #listen() listener.listen(0) #accept() while True: (sock,addr)=listener.accept() #read()  logging.debug("Server connected to:"+str(addr)) bytes=sock.recv(2048) client_data=""   # get data from the client and decode  while len(bytes)>0: client_data+=bytes.decode() bytes=sock.recv(2048) # parse and process the data from the client  list_of_parts=client_data.split(' ') if len(list_of_parts) !=7 and len(list_of_parts)!=9: # signal an error  # error response  response_status = 'E'  response_message= 'Incorrect number of parameter values'  logging.error(response_message) else: try: # try to convert the two value to int  # compute them and create a correct response  xValue = 0 solution = 0 #identifying the message to compute the correct response  if list_of_parts[0][0] == 'E': #get X value from message  xValue = float(list_of_parts[0][1:]) #get the remaining polynomial  rest = list_of_parts[1:] solution = polynomial.evaluate(xValue,rest) response_status = 'E'   #verify if input message is to estimate a solution to a polynomial  if list_of_parts[0][0] == 'S': #get first interval  a = float(list_of_parts[0][1:]) #get second interval  b = float(list_of_parts[1]) #getting the error value  error = float(list_of_parts[(len(list_of_parts)-1)]) #get the polynomial coefficients  rest = list_of_parts[2:(len(list_of_parts)-1)] #computing the bisection  solution = polynomial.bisection(a,b, rest, error) #response status  response_status='S'   #returning the corresponding response  response_message=str(solution) except: # signal a conversion or computation problem  # error response  logging.error("...in exception...") response_status = 'E  '   response_message = ' Conversion or computation problem'  logging.error(response_message) #write()  message=response_status+response_message response_bytes=message #encode and send  sock.sendall(response_bytes.encode()) sock.shutdown(1) #close()  sock.close() 

================================

import socket #socket() sock=socket.socket() #connect() sock.connect(('127.0.0.1', 12345)) #message to evaluate polynomial given x eval= "E1.0 -945 1689 -950 230 -25 1"  #message to find solution of polynomial bisect= "S0 2 -945 1689 -950 230 -25 1 1e-15"  #request value value = input("Enter 'E' to Evaluate a polynomial or 'S' to get an aproximate solution to the polynomial ") #identifying the type of message if value == 'E': request=str(eval) #identifying the type of message if value == 'S': request=str(bisect) #write() Error sock.sendall(request.encode()) # signal the server we're sending no more data sock.shutdown(1) #read() data from server bytes=sock.recv(2048) response=""  #decoding the data while len(bytes)>0: response+=bytes.decode() bytes = sock.recv(2048) #displaying the message print(response) #close() sock.close() ========================= 
 def evaluate(x, poly): if len(poly) == 0: return 0 else: return x*evaluate(x,poly[1:]) + poly[0] def bisection(a, b, poly, tolerance): if evaluate(a, poly) > 0: raise Exception("poly(a) must be <= 0") if evaluate(b,poly) < 0: raise Exception("poly(b) must be >= 0") mid = (a+b) / 2 if abs(b-a) <= tolerance: return mid else: val = evaluate(mid,poly) if val <= 0: return bisection(mid, b, poly, tolerance) else: return bisection(a, mid, poly, tolerance) 

====================================================

#write() Error sock.sendall(request.encode()) 

It give an undefine error

assignment is to

The server will listen on a specific port number (ex. 12345). It will carry out polynomial computations using the functions in the provided module. Requests are in one of two formats:

Evaluate Request o Request starts with E o Followed by an argument value o Followed by a single space o Followed by the coefficients of a polynomial, separated by single spaces

Bisection Request o Requests starts with S o Followed by a, b, polynomial, tolerance separated by single spaces

The server will create a response to the client for each request. The first character of the response will indicate the type of response: X indicates an error response. The remainder of the response is an error message E indicates a successful response to an evaluate request. This is immediately followed by the value returned by the evaluate function S indicates a successful response to a bisection request. This is immediately followed by the value returned by the bisection function

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

Step: 3

blur-text-image

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

Principles Of Multimedia Database Systems

Authors: V.S. Subrahmanian

1st Edition

1558604669, 978-1558604667

More Books

Students also viewed these Databases questions

Question

7. Determine what feedback is provided to employees.

Answered: 1 week ago