def write_file(self, respiter): if sendfile is not None and util.is_fileobject(respiter.filelike): # sometimes the fileno isn't a callable if six.callable(respiter.filelike.fileno): fileno = respiter.filelike.fileno() else: fileno = respiter.filelike.fileno fd_offset = os.lseek(fileno, 0, os.SEEK_CUR) fo_offset = respiter.filelike.tell() nbytes = max(os.fstat(fileno).st_size - fo_offset, 0) if self.response_length: nbytes = min(nbytes, self.response_length) if nbytes == 0: return self.send_headers() if self.is_chunked(): chunk_size = "%X\r\n" % nbytes self.sock.sendall(chunk_size.encode('utf-8')) self.sendfile_all(fileno, self.sock.fileno(), fo_offset, nbytes) if self.is_chunked(): self.sock.sendall(b"\r\n") os.lseek(fileno, fd_offset, os.SEEK_SET) else: for item in respiter: self.write(item)