Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Client/Server Using Sockets Develop two Python scripts: one a server; the other a client that will use the server. You will use the polynomial function

Client/Server Using Sockets

Develop two Python scripts: one a server; the other a client that will use the server. You will use the polynomial function module polynomials.py. Do not include those functions directly into your server code, but import them into the server (see Additional Notes below).

polynomial.py

""" Polynomial manipulations. Polynomials are represented as lists of coefficients, 0 order first. """

def evaluate(x, poly): """ Evaluate the polynomial at the value x. poly is a list of coefficients from lowest to highest.

:param x: Argument at which to evaluate :param poly: The polynomial coefficients, lowest order to highest :return: The result of evaluating the polynomial at x """

if len(poly) == 0: return 0 else: return x*evaluate(x,poly[1:]) + poly[0]

def bisection(a, b, poly, tolerance): """ Assume that poly(a) <= 0 and poly(b) >= 0. Modify a and b so that abs(b-a) < tolerance and poly(b) >= 0 and poly(a) <= 0. Return (a+b)/2 :param a: poly(a) <= 0 :param b: poly(b) >= 0 :param poly: polynomial coefficients, low order first :param tolerance: greater than 0 :return: an approximate root of the polynomial """ 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)

Server

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

For example, here is a sample evaluate request:

E1.0 -945 1689 -950 230 -25 1

This is a request to evaluate the polynomial -945 + 1689x - 950x2 + 230x3 - 25x4 + x5 at the value x = 1.0

Here is a sample bisection request:

S0 2 -945 1689 -950 230 -25 1 1e-15

This requests the use of the bisection function with a = 0, b = 2, tolerance = 1e-15 and using the same polynomial as in the evaluate example.

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

The response to the example evaluate request would be2 E2.2737367544323206e-13

The response to the example bisection request would be S1.0000000000000004

The server should operate in a continuing manner: the server should repeatedly accept a connection, get a request, send a response and close the connection to the client. In particular, only one request is handled for each connection.

Server Error Checking

The server should respond properly in the case of an erroneous request. Not only should the server not crash but the server should send back an appropriate error response to the client.

For example, the request string E1.0 -945xx 1689 -950 230 -25 1 should result in a response something like this:

Xinvalid format numeric data

Note the X that flags this as an error message.

Include error checking in the server. Also document the error checking by including a string at the end of the server code that describes the errors caught. That string might look something like this (to start with):

''' Invalid numeric format Wrong number of fields in request '''

Client

You are strongly urged to write small clients that will test aspects of your server as you work on that. Once you are satisfied that you can send an evaluation request and get a good result and also send a bisection request and get a good result then work on the client described here.

The client should define four variables at the beginning: the first, named poly with a list of numbers representing the coefficients of a polynomial;3 another, named a, representing a value a to use in bisection; another, named b representing a value b to use in bisection; another, named tol representing a tolerance value to use in bisection.

Note: in testing your client, the values assigned to these variables will be changed so make sure they are evident.

The client should first make a request to the server for a bisection. Provide the data defined in the variables just described. Display the value returned.

The result from the first request should be used as the x value in another request to the server to evaluate the polynomial (defined in the variable described above). The result of the evaluation should be displayed. This value should be very close to 0.

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

Fundamentals Of Database System

Authors: Elmasri Ramez And Navathe Shamkant

7th Edition

978-9332582705

More Books

Students also viewed these Databases questions