def put_file(self, file_obj, size=None): """ PUTs the file() object <file_obj> on the server. If the object has a fileno(), we will fstat it to determine its size and send it along the request such that the server can preallocate the space. If a size is specified via <size>, we will send that size instead. However, we will still send as much as we can read, whether that is less or more than size. """ sock = self._connect() f = sock.makefile() if size is None and hasattr(file_obj, 'fileno'): stat_size = os.fstat(file_obj.fileno()).st_size if stat_size != 0: size = stat_size if size is None: f.write(struct.pack("B", BERTHA_PUT)) else: f.write(struct.pack("<BQ", BERTHA_SPUT, size)) while True: buf = file_obj.read(4096) if not buf: break f.write(buf) f.flush() sock.shutdown(socket.SHUT_WR) key = f.read(32) f.close() sock.close() return to_hex(key)
def finish(self): """ Finish the PUT. Returns the key of the blob """ self.f.flush() self.__sock.shutdown(socket.SHUT_WR) key = self.f.read(32) self.f.close() self.__sock.close() self.f = None return to_hex(key)
def put_str(self, s): """ PUTs a string on the server """ sock = self._connect() f = sock.makefile() f.write(struct.pack("<BQ", BERTHA_SPUT, len(s))) f.write(s) f.flush() sock.shutdown(socket.SHUT_WR) key = f.read(32) f.close() sock.close() return to_hex(key)
def list(self): """ LISTs all keys on the server. """ sock = self._connect() f = sock.makefile() f.write(struct.pack("B", BERTHA_LIST)) f.flush() ret = [] buf = to_hex(f.read()) i = 0 while len(buf) >= i + 64: ret.append(buf[i:i+64]) i += 64 return ret
def list_iter(self): """ LISTs all keys on the server. This function returns an iterator. """ sock = self._connect() f = sock.makefile() f.write(struct.pack("B", BERTHA_LIST)) f.flush() while True: s = f.read(32) if len(s) != 32: break yield to_hex(s) f.close() sock.close()