def __init__(self, port): self.port = port # Create the UDP broadcast port while True: try: self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) break except: print("Send socket open error: " + os.strerr(error.errno) + "\n", file=sys.stderr) time.sleep(1)
def _make_symlink(dbgap_file): """Make (relative path) symlinks to a DbgapFile object's path in the current directory. Arguments: dbgap_file: a DbgapFile object whose path will be used to make a symlink """ path = os.path.relpath(dbgap_file.full_path) if not os.path.exists(path): raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), path) os.symlink(path, dbgap_file.basename) if not _check_symlink(dbgap_file.basename): raise FileNotFoundError(errno.ENOENT, os.strerr(errno.ENOENT), dbgap_file.basename)
def read(self): try: filer = open(self.file_dir, "rb") data = bytearray(filer.read()) filer.close() size, offset = self.get_size_offset(data) self.__show_feedback(f"The size of this BMP file is {size}") return data except FileNotFoundError as e: sys.stderr.write("[ERROR] File not found\n") sys.stderr.flush() exit(e.errno) except IOError as e: sys.stderr.write(f"[ERROR] Could not open file {self.file_dir}: "+\ os.strerr(e.errno)+"\n") sys.stderr.flush() exit(e.errno)
def write(self, data, dst=None): if dst is None: write_dir = self.__dstDir() elif not dst.endswith(".bmp"): self.__show_feedback("Destination file does not correspond to a BMP file"+\ "\nNew destination file will be created","warning") write_dir = self.__dstDir() else: write_dir = dst try: filew = open(write_dir, "wb") filew.write(data) filew.close() self.__show_feedback(f"Data has been written in {write_dir}") except Exception as e: print(e) except IOError as e: sys.stderr.write(os.strerr(e.errno) + "\n") sys.stderr.flush() exit(e.errno)
Read bytearray ''') from os import strerror as strerr data = bytearray(10) for i in range(len(data)): data[i] = ord('a') + i try: bf = open('file.bin', 'wb') bf.write(data) bf.close() except IOError as e: print("I/O error occurred:", strerr(e.errno)) from os import strerror as strerr data = bytearray(10) try: bf = open('file.bin', 'rb') bf.readinto(data) bf.close() for b in data: print(hex(b), end=' ') except IOError as e: print("I/O error occurred:", strerr(e.errno))
try: ser.open() except serial.SerialException as e: sys.stderr.write('Could not open serial port {}: {}\n'.format( ser.name, e)) sys.exit(1) # Start the thread that broadcasts serial to a UDP port ser_to_net = SerialToNet(broadcast_port) serial_worker = serial.threaded.ReaderThread(ser, ser_to_net) serial_worker.start() # Create the UDP receive socket while True: try: recv_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) recv_socket.bind(("", receive_port)) break except: print("Receive socket open error: " + os.strerr(error.errno) + "\n", file=sys.stderr) time.sleep(1) # Start the receive loop while True: data, addr = recv_socket.recvfrom(128) ser.write(data) serial_worker.stop()
print(''' 6.1.9.14 Working with real files Copying files - a simple and functional tool ''') from os import strerror as strerr srcname = input("Source file name?: ") try: src = open(srcname, 'rb') except IOError as e: print("Cannot open source file: ", strerr(e.errno)) exit(e.errno) dstname = input("Destination file name?: ") try: dst = open(dstname, 'wb') except IOError as e: print("Cannot create destination file: ", strerr(e.errno)) src.close() exit(e.errno) buffer = bytearray(65536) total = 0 try: readin = src.readinto(buffer) while readin > 0: written = dst.write(buffer[:readin]) total += written readin = src.readinto(buffer) except IOError as e: