-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
58 lines (47 loc) · 1.78 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from socket import socket
from server import server_port, encoding
__author__ = 'student'
def sendtoserver(host, port, str):
sock = socket()
sock.connect((host, port))
nencoded = str.encode(encoding) # Encode string
sock.sendall(nencoded) # Send enconded string
sock.shutdown(1) # Shutdown the sending side of the socket
response = getserverresponse(sock) # Get the response from the server
sock.close() # Close the socket
return response
def getserverresponse(sock):
response_str = ""
bytes = sock.recv(2048)
while len(bytes) > 0:
response_str += bytes.decode(encoding)
bytes = sock.recv(2048)
return response_str
def buildrequest(type, content):
request = ""
if type == 'E':
request += type + str(content[0])
for c in content[1]:
request += ' ' + str(c)
elif type == 'S':
request += type + str(content[0]) + ' ' + str(content[1])
for c in content[2]:
request += ' ' + str(c)
request += ' ' + str(content[3])
else:
print("You specified an incorrect reqeust type.")
#print(request)
return request
if __name__ == "__main__":
a = 0
b = 2
poly = [-945, 1689, -950, 230, -25, 1]
tol = 1e-15
BISECTION = 'S'
EVALUATE = 'E'
bisection_result = sendtoserver('127.0.0.1', server_port(), buildrequest(BISECTION, [a, b, poly, tol]))
# Bisection result will be the server response which will include a successful 'S' so my request below removes that
# from the buildrequest
evaluate_result = sendtoserver('127.0.0.1', server_port(), buildrequest(EVALUATE, [bisection_result[1:], poly]))
print(bisection_result)
print(evaluate_result)