Last active
October 2, 2017 09:33
-
-
Save yangl1996/3ac7a24a6bf1f25dbd9bcd5513cea413 to your computer and use it in GitHub Desktop.
TCP Echoling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import python_bind | |
| # first exec ./echo-server.sh | |
| def tensorflow_run(dt): | |
| print("processing", dt.decode()) | |
| python_bind.init_cloud() | |
| data = python_bind.recv() | |
| tensorflow_run(data) | |
| python_bind.send('result from tensorflow'.encode()) | |
| print('cloud exited') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /bin/bash | |
| function cleanup { | |
| echo 'gracefully exiting...' | |
| rm echoserver-fifo | |
| } | |
| trap cleanup SIGINT SIGTERM | |
| mknod echoserver-fifo p | |
| # start tcp server | |
| echo 'forwarding 127.0.0.1:12580 to 127.0.0.1:12581' | |
| cat echoserver-fifo | nc -l -k 127.0.0.1 12580 | nc -l -k 127.0.0.1 12581 > echoserver-fifo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import python_bind | |
| # first exec ./echo-server.sh | |
| python_bind.init_edge() | |
| python_bind.send('data from edge'.encode()) | |
| result = python_bind.recv() | |
| print('from cloud:', result.decode()) | |
| print('edge exited') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import socket | |
| client = None | |
| def int_to_bytes(dt): | |
| ba = [] | |
| ba.append(dt & 0xff) | |
| dt = dt >> 8 | |
| ba.append(dt & 0xff) | |
| dt = dt >> 8 | |
| ba.append(dt & 0xff) | |
| dt = dt >> 8 | |
| ba.append(dt & 0xff) | |
| return bytes(ba) | |
| def bytes_to_int(dt): | |
| return int.from_bytes(dt, byteorder='little', signed=False) | |
| def init_edge(): | |
| global client | |
| client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| client.connect(('127.0.0.1', 12580)) | |
| return client | |
| def init_cloud(): | |
| global client | |
| client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| client.connect(('127.0.0.1', 12581)) | |
| return client | |
| # packet format: [ payload size (4 bytes) ] [ payload ] | |
| def send(pld): | |
| payload_len = len(pld) | |
| client.send(int_to_bytes(payload_len)) | |
| client.send(pld) | |
| return | |
| def recv(): | |
| len_bytes = client.recv(4) | |
| payload_len = bytes_to_int(len_bytes) | |
| payload_buffer = bytes() | |
| while True: | |
| received = client.recv(4096) | |
| payload_buffer += received | |
| payload_len -= len(received) | |
| if (payload_len <= 0): | |
| break; | |
| return payload_buffer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment