Create two Python scripts: one a server; the other a client that will use the se
ID: 668300 • Letter: C
Question
Create two Python scripts: one a server; the other a client that will use the server. You will use the polynomial function module you created in the last assignment. Do not include those functions directly into your server code, but import them into the server.1
Server
The server will listen on port 12321. It will carry out polynomial computations using the functions you created in Assignment #1. Requests are in one of two formats:
Evaluate Request
Request starts with ‘E’
Followed by an argument value
Followed by a single space
Followed by he coefficients of a polynomial, separated by single spaces
Bisection Request
Requests starts with ‘S’
Followed by ‘a’, ‘b’, polynomial, tolerance separated by single spaces
For example, here is a sample evaluate request:
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:
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 be
The response to the example balance request would be2
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 connectoin.
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
should result in a response something like this:
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):
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: one with a list value representing a polynomial; another representing a value a to use in bisection; another representing a value b to use in bisection; another representing a tolerance value to use in bisection.
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.
Client/Server Using Sockets
Create two Python scripts: one a server; the other a client that will use the server. You will use the polynomial function module you created in the last assignment. Do not include those functions directly into your server code, but import them into the server.1
Server
The server will listen on port 12321. It will carry out polynomial computations using the functions you created in Assignment #1. Requests are in one of two formats:
Evaluate Request
Request starts with ‘E’
Followed by an argument value
Followed by a single space
Followed by he coefficients of a polynomial, separated by single spaces
Bisection Request
Requests starts with ‘S’
Followed by ‘a’, ‘b’, polynomial, tolerance separated by single spaces
For example, here is a sample evaluate request:
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:
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 be
The response to the example balance request would be2
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 connectoin.
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
should result in a response something like this:
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):
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: one with a list value representing a polynomial; another representing a value a to use in bisection; another representing a value b to use in bisection; another representing a tolerance value to use in bisection.
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.
Functions from assign1
def eval(x, poly):
result = poly[0]
for i in range(0, len(poly)):
result = result + poly[i] * (x**i)
return result
def bisection(a, b, poly, tolerance):
if eval(a, poly) > 0 > eval(b, poly):
raise Exception("Parameters not met")
while abs(b-a) > tolerance:
mid = (a+b)/2
result = eval(mid, poly)
if result <= 0:
a = mid
else:
b = mid
return (a+b)/2
Explanation / Answer
Starting up on localhost port 10000
waiting for connection
connection from ('127.0.0.1','12321')
received 'connection message'
sending data to function
eval(0,-945+1689x-950x2+230x3-24x4+x5)
sending data back to the client
'Invalid Numeric Format'
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.