为什么成功消息在ftplib中被视为错误 [英] Why is a success message considered an error in ftplib

查看:133
本文介绍了为什么成功消息在ftplib中被视为错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  import ftplib 
server ='192.168.1.109'
user ='bob'
password ='likes_sandwiches'

box = ftplib.FTP(服务器)
box.login(用户,密码)

s = box.mkd('\\a\\this4\\' )
box.close()

x = raw_input('done,eat sandwiches now')

返回:

追溯(最近一次调用最后一次):
文件C:\scripts\ftp_test.py ,第25行,在
s = box.mkd('\E\this4\')
文件C:\Python26\lib\ftplib.py,第553行,在mkd
返回parse257(resp)
文件C:\Python26\lib\ftplib.py,第651行,在parse257中
提升error_reply,resp
error_reply :250目录创建成功。



它成功创建了一个目录,但它认为它是一个错误! WTF?



我打算在循环中创建多个目录,如何在不成功创建单个目录时不中断它?

解决方案

ftplib 期望得到257的结果,定义为created解析<路径名> 并返回给你;但你的服务器出乎意料地给出了250的结果,并且不是返回路径名,所以 mkd 方法当然失败了。



作为这种特殊服务器行为的解决方法,您可以使用 voidcmd 只发送 MKD / your / path 命令 - 毕竟,您知道路径名你想创建,因为它是绝对的。


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?

解决方案

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 the mkd 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.

这篇关于为什么成功消息在ftplib中被视为错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆