MCPcopy Create free account
hub / github.com/Apress/python-for-matlab-development / main

Function main

code/IO/tcp/server.py:36–62  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

34from client import wait_for_nBytes
35
36def main():
37 Host, Port = 'localhost', 5006
38
39 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
40 s.bind((Host, Port))
41 s.listen(1)
42
43 conn, addr = s.accept()
44 while True:
45 try:
46 Bytes = wait_for_nBytes(8, conn) # get bytes of N
47 if not Bytes:
48 break
49 N = struct.unpack(f'q', Bytes)[0] # bytes -> N
50 print(f'got N={N}')
51 Bytes = wait_for_nBytes(8*N*N, conn) # get bytes of A
52 A = np.frombuffer(Bytes, dtype=np.float64)
53 # could also do
54 # A = np.array(struct.unpack(f'{N*N}d', Bytes))
55 A = A.reshape(N,N)
56 print(f'received {N} x {N} from client')
57 inv = np.linalg.inv(A)
58 conn.send(inv.tobytes()) # send inv(A)
59 print(f'sent inverse to client')
60 except KeyboardInterrupt:
61 break
62 conn.close()
63if __name__ == "__main__": main()

Callers 1

server.pyFile · 0.70

Calls 1

wait_for_nBytesFunction · 0.90

Tested by

no test coverage detected