我可以使用Python将内存中的对象上传到FTP吗?

发布于 2024-08-29 15:01:22 字数 368 浏览 15 评论 0原文

这就是我现在正在做的事情:

mysock = urllib.urlopen('http://localhost/image.jpg')
fileToSave = mysock.read()
oFile = open(r"C:\image.jpg",'wb')
oFile.write(fileToSave)
oFile.close
f=file('image.jpg','rb')
ftp.storbinary('STOR '+os.path.basename('image.jpg'),f)
os.remove('image.jpg')

将文件写入磁盘然后立即删除它们似乎是系统上应该避免的额外工作。我可以使用Python将内存中的对象上传到FTP吗?

Here's what I'm doing now:

mysock = urllib.urlopen('http://localhost/image.jpg')
fileToSave = mysock.read()
oFile = open(r"C:\image.jpg",'wb')
oFile.write(fileToSave)
oFile.close
f=file('image.jpg','rb')
ftp.storbinary('STOR '+os.path.basename('image.jpg'),f)
os.remove('image.jpg')

Writing files to disk and then imediately deleting them seems like extra work on the system that should be avoided. Can I upload an object in memory to FTP using Python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦中楼上月下 2024-09-05 15:01:22

由于 duck-typing,代码中的文件对象 (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 code.
    """
    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 提供了这样的“内存文件”服务。

Because of duck-typing, the file object (f in your code) only needs to support the .read(blocksize) call to work with storbinary. When faced with questions like this, I go to the source, in this case 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 code.
    """
    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()

As commented, it only wants a file-like object, indeed it not even be particularly file-like, it just needs read(n). StringIO provides such "memory file" services.

梨涡少年 2024-09-05 15:01:22

您可以使用任何内存中类似文件的对象,就像BytesIO

from io import BytesIO

它有效两者均采用二进制模式 FTP.storbinary

f = BytesIO(b"the contents")
ftp.storbinary("STOR /path/file.txt", f)

以及使用 FTP.storlines

f = BytesIO(b"the contents")
ftp.storlines("STOR /path/file.txt", f)

有关更高级的示例,请参阅:

You can use any in-memory file-like object, like BytesIO:

from io import BytesIO

It works both in binary mode with FTP.storbinary:

f = BytesIO(b"the contents")
ftp.storbinary("STOR /path/file.txt", f)

as well as in ascii/text mode with FTP.storlines:

f = BytesIO(b"the contents")
ftp.storlines("STOR /path/file.txt", f)

For more advanced examples, see:

梦屿孤独相伴 2024-09-05 15:01:22
import urllib
import ftplib

ftp = ftplib.FTP(...)
f = urllib.urlopen('http://localhost/image.jpg')
ftp.storbinary('STOR image.jpg', f)
import urllib
import ftplib

ftp = ftplib.FTP(...)
f = urllib.urlopen('http://localhost/image.jpg')
ftp.storbinary('STOR image.jpg', f)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文