Why is a success message considered an error in ftplib

核能气质少年 提交于 2019-12-10 18:06:10

问题


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?


回答1:


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:

if resp[:3] != '257':
    raise error_reply, resp



回答2:


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.



来源:https://stackoverflow.com/questions/3463033/why-is-a-success-message-considered-an-error-in-ftplib

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!