Created
April 10, 2014 18:30
-
-
Save jameskyle/10409710 to your computer and use it in GitHub Desktop.
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
| def stream_handler(sock, address): | |
| """ | |
| The first 8 bytes, 64 bits, should be the size of the transfer in network | |
| byte order. Transfer size type is unsigned 64 bit integer. | |
| Total transfer size T ends up being T + 8. | |
| """ | |
| import math | |
| data = '' | |
| # TODO: allow read size to be configured in settings. | |
| log.debug("Received connection from {0}".format(address)) | |
| while len(data) < 8: | |
| data += sock.recv(4096) | |
| length = struct.unpack('!Q', data[:8])[0] | |
| log.debug("Received flow length of {0}".format(length)) | |
| data = '' | |
| start = time.time() | |
| total = 0 | |
| while total < length: | |
| received = len(sock.recv(4096)) | |
| total += received | |
| megabytes = total / 1024 / 1024 | |
| #log.debug("Sent {0} megabytes so far".format(megabytes)) | |
| mbs = megabytes / (time.time() - start) | |
| print("Receiving {0} megabytes / second ".format(mbs), end="\r") | |
| log.debug("Handler done. Received {0} bytes in {1} seconds".format( | |
| total, | |
| time.time() - start | |
| )) | |
| def start_stream_server(): | |
| log.debug("Starting Stream Server.") | |
| stream = StreamServer(('0.0.0.0', 9092), stream_handler) | |
| stream.serve_forever( |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment