python - '550 系统找不到指定的文件' 使用方法 request.urlopen 获取 FTP 上的文件
问题描述
当我调用 request.urlopen() 方法时出现编码问题。urllib.request.ftpwrapper init() 和 retrfile() 方法中的 ftplib.FTP() 实例使用默认 latin-1,我需要在 utf-8 和 cp1251 之间进行选择,我看到 3 种方式:
- 我想要的方式,但不知道如何。
使用包含编码的参数调用 request.urlopen()。并且该编码必须写入 self.ftp.encoding (ftplib.FTP())
- 我不喜欢的方式。
从 ftp (ftp lib) 获取文件名编码并在 request.urlopen(url.encode(file_name_encoding).decode('latin-1')) 中使用它。
问题描述。我有一个名称中包含西里尔文(rus)字符的文件。脚步:
连接到 FTP
con = ftplib.FTP()
con.connect(host, port)
con.login(username, password)
获取文件列表
list_files = [_v for _v in self.con.nlst(_path)]
['Message.xml', 'Message_ÁÏ_TT.xml']
(对于文件 Message.xml、Message_БП_TT.xml)
在第一步使用修复它
con.encoding = 'utf-8'
con.sendcmd('OPTS UTF8 ON')
然后我需要使用:
from urllib import request
url = 'ftp://login:password@ftpaddr:21/folder//Message_БП_TT.xml'
request.urlopen(url.encode().decode('latin-1'))
然后得到异常:
{URLError}<urlopen error ftp error: URLError("ftp error: error_perm('550 系统找不到指定的文件。')")>
在请求库中有 init() 和 retrfile() 用于初始化 ftp 连接。而且我看不到如何更改 ftp 默认编码“latin-1”。
使用此方法是因为使用 urllib.response.addinfourl 解析繁重的 xml 文件。
PS 对于某些 FTP,此方法效果很好,并且可以成功读取文件。他们中的一些人得到了这个例外。原因尚不清楚。并且无法获取和分析 FTP 设置。
解决方案
我不喜欢的解决方案。
据我了解,FTP 上的文件名可以是 utf-8 或 cp1251 (win-1251) 编码。当 ftplib 使用标准编码(latin-1)初始化时,它看起来像:
消息_ÐÐ_TT.xml - utf-8
Message_ÁÏ_TT.xml - cp1251
我不知道在发出请求时在 ftp 上使用什么编码,并且总是使用 utf-8 (encode())。所以我不喜欢它,但它有效:
try:
return request.urlopen(url.encode('utf-8').decode('latin-1'))
except URLError:
return request.urlopen(url.encode('cp1251').decode('latin-1'))
PS utf-8 下尝试清楚
推荐阅读
- matlab - 在 For 循环中定义变量
- botframework - 如何知道在使用 Microsoft Teams 和 Action.Submit 时按下了哪个自适应卡片按钮
- python - 使用常量联系 API
- perl - 使用 Perl 删除除最新缓存文件之外的所有缓存文件
- javascript - 如何使用 JQuery 或 Javascript 设置日期类型值的输入
- javascript - 如何在条形图 js 3.x 上显示数据值?
- ruby-on-rails - 跨整个模型而不是用户记录的弹性搜索
- scala - when() 子句中的 Scala 多个条件
- react-native - 如何覆盖 NativeBase 上的默认主题?
- javascript - 快递.js;在全球范围内使用路由变量