为什么成功消息在 ftplib 中被视为错误
import ftplib
server = '192.168.1.109'
user = 'bob'
password = 'likes_sandwiches'
box = ftplib.FTP(server)
box.login(user, password)
s = box.mkd('\\a\\this4\\')
box.close()
x = raw_input('done, eat sandwiches now')
这将返回:
Traceback(最近一次调用最后一次): 文件“C:\scripts\ftp_test.py”,第 25 行,位于 s = box.mkd('\E\this4\') mkd 中的文件“C:\Python26\lib\ftplib.py”,第 553 行 返回parse257(resp) 文件“C:\Python26\lib\ftplib.py”,第 651 行,在 parse257 中 引发 error_reply, resp error_reply: 250 目录创建成功。
它成功创建了一个目录,但它认为这是一个错误!搞什么?
我计划在循环中创建许多目录,如何才能做到这一点而不让它在每次成功创建单个目录时都中断?
import ftplib
server = '192.168.1.109'
user = 'bob'
password = 'likes_sandwiches'
box = ftplib.FTP(server)
box.login(user, password)
s = box.mkd('\\a\\this4\\')
box.close()
x = raw_input('done, eat sandwiches now')
This returns:
Traceback (most recent call last):
File "C:\scripts\ftp_test.py", line 25, in
s = box.mkd('\E\this4\')
File "C:\Python26\lib\ftplib.py", line 553, in mkd
return parse257(resp)
File "C:\Python26\lib\ftplib.py", line 651, in parse257
raise error_reply, resp
error_reply: 250 Directory created successfully.
It successfully created a directory, but it thinks its an error! WTF?
I plan on creating many directories in a loop, how can I do this without having it break every time it successfully creates a single directory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 RFC 959 (FTP),MKD 的唯一有效响应代码是 257。看来这是由于FTP服务器不符合标准而导致的问题。
为了您的兴趣,这是相关的 ftplib 代码:
According to RFC 959 (FTP), the only valid response code to MKD is 257. Looks like this is a problem caused by the FTP server not conforming to the standard.
For your interest, this is the relevant ftplib code:
ftplib
期望结果为 257,定义为“已创建”,因此它可以解析
并为您返回;但您的服务器令人惊讶地给出了 250 的结果,并且不返回路径名,因此mkd
方法当然会失败。作为解决这种特殊服务器行为的方法,您可以使用 voidcmd 只需发送
MKD /your/path
命令 - 毕竟,您知道要创建的路径名,因为它是绝对路径名。ftplib
is expecting a result of 257, defined as " created", so it can parse the<pathname>
and return it for you; but your server is surprisingly giving a result of 250 and does not return the pathname, so themkd
method of course fails.As a workaround to this peculiar server behavior, you can use voidcmd to just send the
MKD /your/path
command -- after all, you know the pathname you want to create, since it's an absolute one.