Python ftplib 和 storbinary
尝试了解 ftplib 的工作原理。
我正在尝试将文件保存到 FTP 服务器并实现回调。
文档说:
FTP.storbinary(command, file[, blocksize, callback, rest])
callback
函数的定义如文档中所示:
调用回调函数 接收到的每个数据块,带有 给出数据的单个字符串参数 块。
我如何实现这个回调? retrbinary
上的示例回调(读取文件)可能如下所示:
def handle(block):
f.write(block)
print ".",
这将显示下载文件的进度,f
是文件对象。
但我不知道如何使用 storbinary 来实现这一点。
关于如何做到这一点有什么建议吗?我知道 block
参数,但是如何在上传时调整它?
更新:
我有一个上传回调:
def handle(block):
f.read(block)
print ".",
但正如预期的那样,它给出了错误:
需要一个整数
传递int(block)
也不起作用。
Trying to understand how the ftplib
works.
I am trying to save a file to a FTP server and implement a callback.
The documentation says:
FTP.storbinary(command, file[, blocksize, callback, rest])
callback
function is defined as in the documentation:
The callback function is called for
each block of data received, with a
single string argument giving the data
block.
How do I implement this callback? A sample callback on the retrbinary
(reading a file) could look like:
def handle(block):
f.write(block)
print ".",
Which will show the progress of the file being downloaded, f
being the file object.
But I am at a loss on how to implement this with storbinary
.
Any suggestions on how this can be done? I know about the block
parameter, but how do I adjust it with the uploading?
UPDATE:
I have a callback for uploading as:
def handle(block):
f.read(block)
print ".",
But as expected, it gives the error:
an integer is required
Passing int(block)
also doesn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你的回调是
Python 具有可以作为参数传递的第一类函数 - 这是回调的要点 - 你将函数作为参数传递给 storbinary 调用 -
从 python 文档来看,
回调是一个可选的单参数可调用,即发送后调用每个数据块。
它纯粹是一种后处理方法,例如显示传输状态,在发送每个数据块后调用它。上面每发送 1024 字节数据后就会调用它。
要实现传输状态,类似
os.path.getsize 将为您提供文件的总大小(以字节为单位)。
If your callback is
Python has first class functions that can be passed as params- this is the point of a callback- you you pass the function as param to the storbinary call-
From the python doc,
callback is an optional single parameter callable that is called on each block of data after it is sent.
It's purely a post-processing method for e.g. showing transfer status, it's called after each block of data is sent. Above it would be called after sending every 1024 bytes of data.
To implement transfer status, something like this-
os.path.getsize will give you the total size in bytes of your file.