def start(self): self.tcp_sock.listen(5) while True: (cs, addr) = self.tcp_sock.accept() print("Client connected from " + str(addr[0])) data = cs.recv(1024) data = data.decode() # expected to receive a string try: ci = ClientInput(data) if ci.t == 'E': print("Client requesting evaluation") ans = polynomials.evaluate(ci.arg1, ci.arguments) msg = "E" + str(ans) if ci.t == 'S': print("Client requesting bisection") print("a: " + str(ci.arg1)) print("b: " + str(ci.arg2)) print("tol: " + str(ci.tol)) print("arguments: " + str(ci.arguments)) ans = polynomials.bisection(ci.arg1, ci.arg2, ci.arguments, ci.tol) msg = "S" + str(ans) #print (ans) except Exception: print("Client sent invalid data") msg = "XError parsing data" cs.sendall(msg.encode())
def Bisection(Identifier, DataArray): DataArray = DataArray.replace(Identifier,"") print("Received data: " + DataArray) DataArray = DataArray.split(' ') print("After Splitting: ") print(DataArray) aValue = float(DataArray[0]) bValue = float(DataArray[1]) tolerance = DataArray[-1] tolerance = float(eval(tolerance)) newValues= [] #Checking for ints, both positive and negative in the array #While skipping the first two and last values since they correspond #to a, b, and the tolerance for s in range(len(DataArray)-3): newValues.append(DataArray[s+2]) newValues = map(int,newValues) print("Array as an int[]: ") print(newValues) print("a: " + str(aValue)) print("b: " + str(bValue)) print("tolerance: " + str(tolerance)) result = str(polynomials.bisection(aValue,bValue,newValues,tolerance)) clientsocket.send("E"+result)
logging.error("Invalid request syntax |{}|".format(data_string)) response = 'XInvalid request syntax, Your request either did not start with an E or S,' \ ' or you had too few arguments' else: if values[0][0] == ACCEPTABLE_REQUEST_TYPES[0]: try: x = float(values[0][1:]) poly = [float(x) for x in values[1:]] result = polynomials.evaluate(x, poly) logging.info("Evaluating {} for {}".format(x, poly)) print("Result: ", result) response = "E" + str(result) except: response = 'Xinvalid numeric format' elif values[0][0] == ACCEPTABLE_REQUEST_TYPES[1]: try: a = float(values[0][1:]) b = float(values[1]) poly = [float(x) for x in values[2:len(values) - 1]] tolerance = float(values[len(values) - 1]) logging.info("Bisection with a:{}, b{}, poly{}, tolerance{}".format(a, b, poly, tolerance)) result = polynomials.bisection(a, b, poly, tolerance) response = "S" + str(result) except: response = 'Xinvalid numeric format' conn.sendall(response.encode(encoding)) conn.shutdown(1) ## shutdown the sending side conn.close() logging.info("connection closed")
message = "X Invalid format numeric data: " + request[1:] logging.error("response " + message) sock.sendall(message.encode()) sock.shutdown(1) elif request_code == "S": try: parameters = request[1:].split(' ') args = [float(x) for x in parameters] a = args[0] b = args[1] poly = args[2:8] tol = args[8] value = polynomials.bisection(a, b, poly, tol) message = "S" + str(value) logging.info("response " + message) sock.sendall(message.encode()) sock.shutdown(1) except Exception as ex: logging.error("Input value conversion error " + request[1:]) logging.error(str(ex)) message = "X Invalid format numeric data: " + request[1:] logging.error("response " + message) sock.sendall(message.encode()) sock.shutdown(1) else: message = "XInvalid request code: " + request_code
if len(message) == 0: reply = "XInvalid request syntax: There are no arguments" elif message[0] == 'S': tokens = message[1:].split() if len(tokens) < 3: reply = "XInvalid request syntax: wrong number of fields in request: " + str( len(tokens)) else: try: a = float(tokens[0]) b = float(tokens[1]) tol = float(tokens[len(tokens) - 1]) poly = [] for i in range(2, len(tokens) - 1): poly.append(float(tokens[i])) reply = "S" + str(bisection(a, b, poly, tol)) except ValueError: reply = "Xinvalid format numeric data" elif message[0] == "E": tokens = message[1:].split() if len(tokens) < 1: reply = "Wrong number of fields in request" else: try: x = float(tokens[0]) poly = [] for i in range(1, len(tokens)): poly.append(float(tokens[i])) reply = "E" + str(evaluate(x, poly)) except ValueError:
poly = [int(float(x)) for x in parts] evaluate = eval(x, poly) messageToClient = 'E{}'.format(evaluate) except Exception as e: messageToClient = 'Xinvalid format numeric data' #Bisection Request if requestType == 'S': if(len(parts) < 4): messageToClient = 'Not enough arguments' else: try: a = int(float(parts[0])) b = int(float(parts[1])) tol = float(parts[-1]) print('a: {} b: {} tol: {}'.format(a, b, tol)) poly = [int(float(x)) for x in parts[2:-1]] bi = bisection(a,b,poly,tol) messageToClient = 'S{}'.format(bi) except Exception as e: messageToClient = 'Xinvalid format numeric data' if requestType != 'E' and requestType != 'S' and len(data_string) > 1: messageToClient = 'X unknown request' print('To Client: "{}"'.format(messageToClient)) bytes = messageToClient.encode(encoding) conn.sendall(bytes) finally: conn.close()
from polynomials import evaluate, bisection poly = [-945, 1689, -950, 230, -25, 1] x = 1.0 a = 0 b = 2 tol = 1e-15 v = evaluate(x, poly) print(v) r = bisection(a, b, poly, tol) print(r)
elif float(list_of_parts[len(list_of_parts) - 1]) == 0: raise Exception("Invalid tolerance" ) # Tolerance can't be 0 for some reason for i in range(2, len(list_of_parts) - 1): list_of_parts[i] = int(list_of_parts[i]) print("Printing the list of parts after converting") print(list_of_parts) print("aval is: " + str(list_of_parts[0][1:]) + " bval is: " + str(list_of_parts[1]) + " tol is: " + str(list_of_parts[len(list_of_parts) - 1])) result = bisection(float(list_of_parts[0][1:]), float(list_of_parts[1]), list_of_parts[2:len(list_of_parts) - 1], float(list_of_parts[len(list_of_parts) - 1])) print("result " + str(result)) response_status = 'S' response_message = str(result) else: raise Exception( "Incorrect command type" ) # Caught if the first character from client is not E or S except Exception as ex: # signal a conversion problem or computation problem # error response response_status = 'X' response_message = str(ex)
args = [float(x) for x in argsStr] x = args[0] poly = args[1:] result = evaluate(x, poly) print("Evaluation: " + str(result)) print("===============================\n") # Bisection print("========== Bisection ==========") message = "S0 2 -945 1689 -950 230 -25 1 1e-15" argsStr = message[1:].split(' ') args = [float(x) for x in argsStr] a = args[0] b = args[1] poly = args[2:8] tol = args[8] print('a: ' + str(a)) print('b: ' + str(b)) print('poly: ' + str(poly)) print('tol: ' + str(tol)) result = bisection(a, b, poly, tol) print("Bisection: " + str(result)) print("===============================")
# error response response_status = 'X:' response_message = ' Invalid Format Numeric Data to Evaluate!' logging.error(response_message) elif client_data[0] == 'S': try: print("Received Data Before Splitting: " + client_data) list_of_parts = client_data.split(' ') print("Data After Split: " + str(list_of_parts)) aVal = int(list_of_parts[0][1:]) bVal = int(list_of_parts[1]) polyBisec = list(map(int, list_of_parts[2:-1])) tol = float(list_of_parts[-1]) print("a: " + str(aVal) + "\nb: " + str(bVal) + "\nPoly: " + str(polyBisec) + "\nTolerance: " + str(tol)) result = polynomials.bisection(aVal, bVal, polyBisec, tol) print("Bisection calculation completed: " + str(result)) except: # signal an error # error response response_status = 'X:' response_message = ' Invalid Format Numeric Data For Bisection Function!' logging.error(response_message) else: try: # try to convert the two value to int # compute them and create a correct response result = multiply1(int(list_of_parts[0]), int(list_of_parts[1])) response_status = 'B' response_message = str(result) except:
#c.send(error) err(error) print("Shutting down server.") c.close() elif not (all(isinstance(item, int) for item in request[5])): error = ['X', "Invalid number format."] #c.send(error) err(error) print("Shutting down server.") c.close() else: a = request[1] b = request[3] poly = request[5] tol = request[7] temp = polynomials.bisection(a, b, poly, tol) success = ['S', temp] #c.send(success) bisec(success) print("Server finished. Shutting down.") c.close() else: ''' Error checking for wrong request type. ''' temp = "Please enter correct request type." error = ['X', temp] #c.send(error) err(error) print("Shutting down server.") c.close()
try: if len(test_string.split(' ')) is 1: conn.sendall(str.encode("XToo few arguments ")) else: a = float(test_string.split(' ').pop(0)[1]) b = float(test_string.split(' ')[1]) poly = list(map(int, test_string.split(' ')[2:-1])) tole = list(map(float, test_string.split(' ')[-1:])) if tole[0] <= 0.0: conn.sendall(str.encode("XInvalid Tolerance")) else: result = str((polynomials.bisection( a, b, poly, tole[0]))) print("Sending back to client... S" + result) conn.sendall(str.encode("S" + result)) except: conn.sendall( str.encode("Xcould not convert string to float")) if test_string.split(' ').pop(0)[ 0] != "E" and test_string.split(' ').pop(0)[0] != "S": conn.sendall(str.encode("XIncorrect Command Type")) except: conn.sendall(str.encode("Xcould not convert string to float"))
__author__ = 'Harrison Jordan' from polynomials import bisection, eval toler = 1e-14 poly1 = [-945, 1689, -950, 230, -25, 1] # roots are 1, 3, 5, 7, 9 x1 = bisection(0, 2, poly1, 1e-15) # print root and evaluate the polynomial print(x1, eval(x1,poly1)) x2 = bisection(4,2,poly1,toler) print(x2, eval(x2,poly1)) x3 = bisection(4,6,poly1, toler) print(x3, eval(x3,poly1)) x4 = bisection(8,6,poly1, toler) print(x4, eval(x4,poly1)) x5 = bisection(8,100,poly1, toler) print(x5, eval(x5,poly1)) # compare the roots to the expected values print(x1-1, x2-3, x3-5, x4-7, x5-9) print(eval(1, [1,2,2]))