如何使用 ftplib 写入远程服务器上的文件

how to write to a file on a remote server using ftplib

我需要以写入模式打开远程服务器上的文件。

我正在使用以下代码 write/read 文件:

python
import tempfile
import io



class OpenRead(object):

    def _open_tempfile(self):
        self.tfile = tempfile.NamedTemporaryFile()
        # Write data on tempfile.
        self.ftp.retrbinary(
            'RETR %s' % self.filename, self.tfile.write)
        # Get back to start of file, so it would be possible to
        # read it.
        self.tfile.seek(0)
        return open(self.tfile.name, 'r')

    def __init__(self, ftp, filename):
        self.ftp = ftp
        self.filename = filename
        self.tfile = None

    def __enter__(self):
        return self._open_tempfile()

    def __exit__(self, exception_type, exception_value, traceback):
        # Remove temporary file.
        self.tfile.close()

class OpenWrite(object):
    def __init__(self, ftp, filename):
        self.ftp = ftp
        self.filename = filename
        self.data = ''

    def __enter__(self):
        return self

    def __exit__(self, exception_type, exception_value, traceback):
        bio = io.BytesIO()
        if isinstance(self.data, six.string_types):
            self.data = self.data.encode()
        bio.write(self.data)
        bio.seek(0)
        res = self.ftp.storbinary('STOR %s' % self.filename, bio)
        bio.close()
        return res

    def write(self, data):
        self.data += data

def open(ftp, filename, mode='r'):
    """Open a file on FTP server."""
    if mode == 'r':
        return OpenRead(ftp, filename)
    if mode == 'w':
        return OpenWrite(ftp, filename)

但是

python
with open(ftplib.FTP('domain.com', 'username', 'password'), 'file.txt', 'w') as file:
    file.write('hello')

投掷

OpenWrite object has no atribute 'write'", 

尽管上下文管理器应该调用 __enter__ 而调用 _open_tempfile 而 returns 是一个打开的对象,但事实并非如此。

我该如何修复错误或者是否有更好的方法来完成这一切?? 谢谢!

我现在明白了。我正在使用 FTP 下载并重新上传文件,在本地将其编辑为临时文件,然后重新上传包含更改的文件。我的代码肯定不是最有效的,但对我有用。

import tempfile
import io

class WriteModeFtp:
        def __init__(self, ftp, filename, mode="a"):
                self.ftp = ftp
                self.filename = filename
                self.mode = mode
                self.load()
        def load(self):

                self.tempfile = tempfile.NamedTemporaryFile()

                with open(self.tempfile.name, "wb+") as tmp:
                        self.ftp.retrbinary('RETR ' + self.filename, tmp.write)
        def getThis(self):
                return open(self.tempfile.name, self.mode)
        def flush(self):
                self.ftp.storbinary('STOR %s' % self.filename, open(self.tempfile.name, "rb"))
class MultiFtpStream:
        def __init__(self, ftp, mode="r"):
                self.ftp = ftp
                self.mode = mode
        def loadFile(self, filename):
                return WriteModeFtp(self.ftp, filename, self.mode)