由于使用鸭式输入,因此文件对象(
f在您的代码中)仅需要支持
.read(blocksize)与一起使用的调用
storbinary。当遇到这样的问题时,我转到源代码,在本例中为lib
/ python2.6 / ftplib.py:
def storbinary(self, cmd, fp, blocksize=8192, callback=None): """Store a file in binary mode. A new port is created for you. Args: cmd: A STOR command. fp: A file-like object with a read(num_bytes) method. blocksize: The maximum data size to read from fp and send over the connection at once. [default: 8192] callback: An optional single parameter callable that is called on on each block of data after it is sent. [default: None] Returns: The response pre. """ self.voidcmd('TYPE I') conn = self.transfercmd(cmd) while 1: buf = fp.read(blocksize) if not buf: break conn.sendall(buf) if callback: callback(buf) conn.close() return self.voidresp()如前所述,它只需要一个类似文件的对象,实际上它甚至不需要,特别是类似于文件的对象
read(n)。StringIO提供了这种“内存文件”服务。



