Python FTP 从 as400 获取文件



我有一个简单的代码(使用Flask-SocketIO和ftplib(用于从as400机器下载csv文件,但我得到了一个我不明白的异常...

你知道问题是什么吗?

我拥有的代码:

try:
localFile = open(localDir + '/' + iso + '/' + output, 'w')
except:
emit('update-status', (iso, 'Error: Couldn't create file ' + localDir + '/' + iso + '/' + output + ' !'))
return
print('RETR ' + as400Dir + '/' + output)
try:
ftp.retrlines('RETR ' + as400Dir + '/' + output, localFile.write)
except ftplib.all_errors as e:
# except Exception as e:
emit('update-status', (iso, 'Error: Couldn't download file ' + as400Dir + "/" + output + ' ! -> ' + str(e)))
return
localFile.close()

控制台输出:

RETR /xreff/pgmref.csv
Exception in thread Thread-5:
Traceback (most recent call last):
File "D:LocalDataxxxInstallPython35-32libthreading.py", line 914, in _bootstrap_inner
self.run()
File "D:LocalDataxxxInstallPython35-32libthreading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "D:LocalDataxxxInstallPython35-32libsite-packagessocketioserver.py", line 452, in _handle_event_internal
r = server._trigger_event(data[0], namespace, sid, *data[1:])
File "D:LocalDataxxxInstallPython35-32libsite-packagessocketioserver.py", line 481, in _trigger_event
return self.handlers[namespace][event](*args)
File "D:LocalDataxxxInstallPython35-32libsite-packagesflask_socketio__init__.py", line 236, in _handler
*args)
File "D:LocalDataxxxInstallPython35-32libsite-packagesflask_socketio__init__.py", line 618, in _handle_event
ret = handler(*args)
File "./index.py", line 453, in updateDatabase
downloadResourceFiles(iso, user, password, xreff, gafint, sugar)
File "./index.py", line 545, in downloadResourceFiles
downloadFiles()
File "./index.py", line 523, in downloadFiles
ftp.retrlines('RETR ' + as400Dir + '/' + output, localFile.write)
File "D:LocalDataxxxInstallPython35-32libftplib.py", line 467, in retrlines
with self.transfercmd(cmd) as conn, 
AttributeError: __exit__

如果我尝试将异常作为 e 而不是将 ftplib.all_errors 作为 e,我会得到:

RETR /xreff/pgmref.csv
emitting event "update-status" to b563563a763e48fbba2d394ae8c871e7 [/]
b563563a763e48fbba2d394ae8c871e7: Sending packet MESSAGE data 2["update-status","NL","Error: Couldn't download file /xreff/pgmref.csv ! -> __exit__"]

我能够重现您的问题。这似乎是事件中的一个错误。你没有说你是否将eventlet与Flask-SocketIO结合使用,但鉴于这是文档推荐的,我假设你是。

下面是一个简单的 eventlet 脚本,其失败方式与 ftplib 相同:

import eventlet
eventlet.monkey_patch()
import socket
with socket.create_connection(('google.com', 80)) as s:
pass

运行此脚本的输出:

$ python test.py
Traceback (most recent call last):
File "test.py", line 6, in <module>
with socket.create_connection(('google.com', 80)) as s:
AttributeError: __enter__

问题是 eventlet 的套接字类没有使其能够用作上下文管理器的__exit__方法。我向事件小程序报告了一个问题:https://github.com/eventlet/eventlet/issues/430

请注意,此错误仅发生在 Python 3 中。Python 2 中不存在对socket.create_connection()的上下文管理器支持,因此在该版本中,retrlines()的 ftplib 实现以不同的方式完成。

最新更新