def server(port): sock = YARUSocket() sock.bind(("127.0.0.1", port)) while True: data, address = sock._sock.recvfrom(65000) text = YARUSocket.parse_packet(data) print(text)
def client(port): sock = YARUSocket() sock.connect(("localhost", port)) sock.write(b"supp") time.sleep(0.25) sock.write(b"bois1") sock.write(b"bois2") sock.write(b"bois3") sock.write(b"bois4") # will raise buffer error
def server(port=1600): sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind(("127.0.0.1", port)) print("Listening at {}".format(sock.getsockname())) data, address = sock.recvfrom(MAX_BYTES) text = YARUSocket.parse_packet(data) print(text)
def receiver(address: ipaddress.IPv4Address, port: int, directory: Path, **kwargs): sock = YARUSocket() sock.bind((str(address), port)) logging.info(f"Receiver bound to {address=}, {port=}") directory.mkdir(exist_ok=True) file_name = None file = None while True: data = sock.read() if not data: if not file_name: time.sleep(1) # Idle mode else: time.sleep(0.1) # active connection continue if data.startswith(NAME_MARKER): # first packet has filename file_name = data[len(NAME_MARKER):].decode() file = open(directory / file_name, "wb") logging.info(f"Receiving {file_name}") size, start_time = 0, time.time() elif data == END_MARKER: # finished sending data delta_time = time.time() - start_time file.close() break else: # file contents received, store to file size += len(data) file.write(data) sock.close() logging.info(f"Completed receiving {file_name}, {size} bytes, " f"{delta_time:.3f} seconds, speed={size / delta_time:.3f}BPS") if 'result' in kwargs: kwargs['result']['size'] = size kwargs['result']['time'] = delta_time
def server(port): sock = YARUSocket() sock.bind(("127.0.0.1", port)) while True: time.sleep(1) print(sock.read())
def client(port): sock = YARUSocket() sock.connect(("localhost", port)) sock.write(b"supp") time.sleep(0.25) sock.write(b"bois1") time.sleep(1) sock.write(b"bois2") sock.write(b"bois3") time.sleep(2) sock.write(b"bois4")
def sender(address: ipaddress.IPv4Address, port: int, file: Path, **kwargs): sock = YARUSocket() sock.connect((str(address), port)) logging.info(f"Sending {file.name}") sock.write(NAME_MARKER + str(file.name).encode("utf-8")) file_data = file.read_bytes() total_size = len(file_data) pkt_size = YARUSocket.MAX_DATA_SIZE offset = 0 while offset < total_size: try: sock.write(file_data[offset:offset + pkt_size]) except Exception: time.sleep(1) else: offset += pkt_size sock.write(END_MARKER) sock.close() logging.info(f"Completed sending {file.name}, {total_size} bytes")
def client(port): sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) data = YARUSocket.make_packet(4, b"S" * 65481) sock.sendto(data, ("127.0.0.1", port))