简体   繁体   English

FTP文件名编码

[英]FTP filename encoding

Hi I use twisted library to connect to FTP server but I have problem with filename encoding. 嗨,我使用扭曲库连接到FTP服务器,但文件名编码存在问题。 I receive 'Illusion-N\\xf3z.txt' so its not unicode. 我收到“ Illusion-N \\ xf3z.txt”,因此不是unicode。 Is there any FTP command to force specific encoding? 是否有任何FTP命令强制执行特定编码? Thanks in advance! 提前致谢! MK MK

There are two possibilities: 有两种可能性:

  • FTP is not unicode aware. FTP不支持Unicode。 It looks like the server you're talking to in this example is sending Latin-1 encoded bytes. 在此示例中,您正在交谈的服务器似乎正在发送Latin-1编码的字节。 So you need to decode the bytes using that encoding when you receive them. 因此,您需要在接收字节时使用该编码对字节进行解码。
  • There is an RFC which updates FTP to be UTF-8-aware. 一个RFC将FTP更新为支持UTF-8。 Check the results of the FEAT command to see if UTF8 is there (but it probably isn't, since the example bytes are not valid UTF-8). 检查FEAT命令的结果以查看是否存在UTF8 (但可能不存在,因为示例字节不是有效的UTF-8)。 If it is, decode the bytes using UTF-8. 如果是这样,请使用UTF-8解码字节。

Twisted's FTP client won't do anything unicode-related for it, since it just implements the basic FTP RFC. Twisted的FTP客户端不会执行任何与Unicode相关的操作,因为它仅实现了基本的FTP RFC。

FTP ignores encodings; FTP忽略编码; as long as a filename does not contain a '\\0' (null character) and '/' (slash) separates directories, it happily accepts anything. 只要文件名不包含'\\0' (空字符)和'/' (斜杠)分隔目录,它就很乐意接受任何内容。

Do your own decoding and encoding of the filenames. 对文件名进行自己的解码和编码。 It is quite probable that the encoding used in your example is "cp1252", which is the “Windows Western” or something like that. 您的示例中使用的编码很有可能是“ cp1252”,即“ Windows Western”或类似名称。

In your case, when you receive 'Illusion-N\\xf3z.txt', convert it to Unicode by 'Illusion-N\\xf3z.txt'.decode('cp1252') . 对于您的情况,当您收到'Illusion-N \\ xf3z.txt'时,请通过'Illusion-N\\xf3z.txt'.decode('cp1252')将其转换为Unicode。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM