开发者

Why is a success message considered an error in 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 s开发者_运维知识库andwiches 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?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜