1

I have a request to handle with filename list on ftp server. But filename includes Asian character and other unknown characters. So I need to judge which filename can be decoded by gb2312, which can be decoded by iso-8859-1. That means if the filename list cannot be gotten using gb2312, then use iso-88591-1 to get. So I don't know how to write code in the following function which is in ftplib

def retrlines(self, cmd, callback = None):
    """Retrieve data in line mode.  A new port is created for you.

    Args:
      cmd: A RETR, LIST, NLST, or MLSD command.
      callback: An optional single parameter callable that is called
                for each line with the trailing CRLF stripped.
                [default: print_line()]

    Returns:
      The response code.
    """
    if callback is None: callback = print_line
    resp = self.sendcmd('TYPE A')
##################I need to update here############################
    with self.transfercmd(cmd) as conn, \
             conn.makefile('r', encoding='iso-8859-1') as fp:
###################################################################
        while 1:

            line = fp.readline()
            print(line)

            if self.debugging > 2: print('*retr*', repr(line))
            if not line:
                break
            if line[-2:] == CRLF:
                line = line[:-2]
            elif line[-1:] == '\n':
                line = line[:-1]
            callback(line)
    return self.voidresp()
4

1 回答 1

0

您没有包含太多代码,因此很难准确判断发生了什么。但作为一般规则,如果您正在与之交互的数据在其编码使用方面不一致,您将不得不以二进制模式与之交互。

所以尽量不要传入编码。希望这会给您返回字节数据,然后您可以根据每个文件的需要进行编码/解码。

于 2012-10-27T08:08:29.963 回答