问题描述
我正在考虑使用ftplib(可能还会使用 ftputil )一些自动的FTP文件同步.目前,我有几台服务器可以对此进行测试,但是,尽管我与两台服务器都进行了成功的对话,但每次错误回复时我都会收到EOFError-s.例如:如果我尝试使用不正确的用户名/密码登录,我将收到所有内容的530响应...但是我还会收到EOFError;如果我使用正确的用户名/密码登录或尝试这样做后尝试登录dir()等,则不会收到EOFError.
I'm looking into using ftplib (and possibly ftputil) for doing some automated FTP file syncing. I have a couple of servers to test this against at the moment, but, whilst I'm having a successful conversation with both servers, I get EOFError-s with each error reply. For example: if I try to log in with an incorrect user/pass, I will get the 530 response with everything... but I also get an EOFError; if I login with a correct user/pass or try to dir() after doing so etc., I get no EOFError.
它似乎只显示错误消息.我强烈怀疑这可能是由服务器而不是python引起的:我在其他地方都未提及此问题.但是,我对服务器设置几乎没有控制.
It seems to only appear with error messages. I strongly suspect this may be caused by the servers rather than python: I've not found any mention of this issue elsewhere. I, however, have very little control over the server setup.
我正在寻求想法:
- 您首先知道是什么原因导致错误吗?
- 如果是在服务器端,您能更具体一点吗?我不知道自己能做什么,直到我知道它是什么...
- 您认为我应该如何处理?我想我可以在每次处理异常之前添加一个
except EOFError: pass
,但是如果您有更好/更简洁的想法,我希望听到他们.
- Do you know what could be causing the error in the first place?
- If it's server-side, could you be more specific? I won't know if I'll be able to do anything about it until I know what it is...
- How do you think I should handle this? I guess I could add an
except EOFError: pass
before each time I handle an exception, but if you have better/neater ideas I would love to hear them.
谢谢!
推荐答案
服务器正在发送EOF
来告诉您它们已经终止了连接.
The servers are sending EOF
to tell you that they've terminated the connection.
除了显然需要使用except EOFError
进行处理之外,您应该将此处理与其他任何断开连接事件一样对待.
You should treat this no differently than any other disconnection event, except that obviously you need to handle it with except EOFError
.
请参见 http://svn .python.org/view/python/trunk/Lib/ftplib.py?view = markup
# Internal: return one line from the server, stripping CRLF.
# Raise EOFError if the connection is closed
182 def getline(self):
183 line = self.file.readline()
184 if self.debugging > 1:
185 print '*get*', self.sanitize(line)
186 if not line: raise EOFError
187 if line[-2:] == CRLF: line = line[:-2]
188 elif line[-1:] in CRLF: line = line[:-1]
189 return line
仅当连接上的readline()
返回空白行时,才会引发EOFError,该空白行表示注释为断开连接事件.
EOFError is only raised when readline()
on the connection returns a blank line, which the comment indicates is a disconnection event.
编辑您的评论:
服务器未发送空行. readline()
返回所有内容,直到下一个\n
或\r
或\r\n
或所有上述内容,具体取决于其配置方式.在这种情况下,没有要读取的内容,因为已到达文件末尾.这会导致readline()
返回空白行,并不意味着已读取空白行.如果已读取空白行,则readline()
将返回以该行结尾的字符(\n
或\r
或\n\r
).
The server doesn't send an empty line. readline()
returns everything up to the next \n
or \r
or \r\n
or all of the abouve depending on how it's configured. In this case, there is nothing to read because the end of the file has been reached. This causes readline()
to return a blank line, it doesn't mean a blank line has been read. If a blank line had been read, readline()
would return the character that ended the line (\n
or \r
or \n\r
).
如果在使用FTPUtil时未得到异常,那是因为它是内部处理的.
If you don't get the exception when using FTPUtil, that is because it handles it internally.
这篇关于使用ftplib时获取EOFError以及异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!