def make_client(address, file_path, chunk_size): logger.info('client running') tcp = TCP() tcp.start() conn = dial(address) logger.info('client connected to server') data = [] while True: chunk = recv(conn, chunk_size) if len(chunk) == 0: break logger.info(f'chunk received. length: {len(chunk)}') data.append(chunk) data = b''.join(data) with open(file_path, 'wb') as fp: fp.write(data) logger.info(f'data saved. length: {len(data)}')
def make_server(address, file_path, chunk_size): logger.info('server running') tcp = TCP() tcp.start() executor = ThreadPoolExecutor() connections = [] server = listen(address) while True: try: conn = accept(server) future = executor.submit(handle, conn, file_path, chunk_size) connections.append((conn, future)) except KeyboardInterrupt: logger.info('closing server') break except Exception as e: logger.exception(e) logger.info('releasing resources') executor.shutdown(True)
import argparse from trapy.tcp.tcp import TCP parser = argparse.ArgumentParser() parser.add_argument("-listen", required=False, type=str, default="", help="Listening host of transport layer") args = parser.parse_args() tcp = TCP(args.listen) tcp.start() tcp.wait()