gpt4 book ai didi

Python NamedTemporaryFile - 读取时出现值错误

转载 作者:行者123 更新时间:2023-12-01 03:49:39 25 4
gpt4 key购买 nike

我在用 Python 写入 NamedTemporaryFile 然后读回时遇到问题。该函数通过 tftpy 将文件下载到临时文件,读取该文件,对内容进行哈希处理,然后将哈希摘要与原始文件进行比较。有问题的函数如下:

def verify_upload(self, image, destination):
# create a tftp client
client = TftpClient(ip, 69, localip=self.binding_ip)
# generate a temp file to hold the download info
if not os.path.exists("temp"):
os.makedirs("temp")
with NamedTemporaryFile(dir="temp") as tempfile, open(image, 'r') as original:
try:
# attempt to download the target image
client.download(destination, tempfile, timeout=self.download_timeout)
except TftpTimeout:
raise RuntimeError("Could not download {0} from {1} for verification".format(destination, self.target_ip))
# hash the original file and the downloaded version
original_digest = hashlib.sha256(original.read()).hexdigest()
uploaded_digest = hashlib.sha256(tempfile.read()).hexdigest()
if self.verbose:
print "Original SHA-256: {0}\nUploaded SHA-256: {1}".format(original_digest, uploaded_digest)
# return the hash comparison
return original_digest == uploaded_digest

问题是,每次我尝试执行行 uploaded_digest = hashlib.sha256(tempfile.read()).hexdigest() 时,应用程序都会错误并显示 ValueError - I/O 对已关闭文件的操作。由于 with block 不完整,我很难理解为什么临时文件会被关闭。我能想到的唯一可能性是 tftpy 在下载后关闭文件,但我在 tftpy 源代码中找不到发生这种情况的任何点。请注意,我还尝试插入行 tempfile.seek(0) 以使文件恢复到正确的读取状态,但这也给了我 ValueError .

tftpy 是否可能关闭文件?我读到 NamedTemporaryFile 中可能存在错误导致此问题?为什么在 with block 定义的引用超出范围之前关闭文件?

最佳答案

TFTPy 正在关闭文件。当您查看源代码时,您错过了以下代码路径:

class TftpClient(TftpSession):
...
def download(self, filename, output, packethook=None, timeout=SOCK_TIMEOUT):
...
self.context = TftpContextClientDownload(self.host,
self.iport,
filename,
output,
self.options,
packethook,
timeout,
localip = self.localip)
self.context.start()
# Download happens here
self.context.end() # <--

TftpClient.download来电 TftpContextClientDownload.end :

class TftpContextClientDownload(TftpContext):
...
def end(self):
"""Finish up the context."""
TftpContext.end(self) # <--
self.metrics.end_time = time.time()
log.debug("Set metrics.end_time to %s", self.metrics.end_time)
self.metrics.compute()

TftpContextClientDownload.end来电 TftpContext.end :

class TftpContext(object):
...
def end(self):
"""Perform session cleanup, since the end method should always be
called explicitely by the calling code, this works better than the
destructor."""
log.debug("in TftpContext.end")
self.sock.close()
if self.fileobj is not None and not self.fileobj.closed:
log.debug("self.fileobj is open - closing")
self.fileobj.close() # <--

TftpContext.end关闭文件。

关于Python NamedTemporaryFile - 读取时出现值错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38465418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com