Python的ftplib STOR可靠吗?
我正在使用此代码将 myfile.txt 从我的 Windows 计算机上传到 ftp 服务器。 upoad 后,脚本会删除本地计算机上的文件(我不会在 ftp 上删除它)。
try:
ftp = FTP(ftp.host.com)
ftp.login(your_username, your_password)
file = open(myfile.txt, "rb")
ftp.storbinary('STOR myfile.txt', file)
print 'STORing File now...'
ftp.quit()
file.close()
subprocess.Popen('del myfile.txt', shell=True)
print 'File deleted'
except all_errors:
print 'An error occured'
这段代码可以运行,但它不可靠!每上传大约 10 次,我的脚本就会在存储文件时挂起。
print 'STORing File now...' # So I just get 'STORING File now...'
文件不大,应该在几秒钟内上传,但我经常需要等待一两个小时,然后才会抛出异常:
print 'An error occured'
如果“更早”抛出异常,那就太好了,所以我可以重新启动上传(例如在 while 循环中)。因为我需要尽快上传这个文件,所以我需要让文件上传更快(我不想等那么久才抛出异常)
第二个问题:有时会发生这种情况:文件上传成功后,脚本无法删除本地计算机上的文件,因为“其他进程已经在访问它”<-我认为 ftplib 没有“释放”该文件。我可以做什么来防止这种情况发生?
我正在寻找更好/可靠的简单文件上传解决方案。有人有主意吗? 谢谢!
I'm using this code to upload myfile.txt from my windows machine to a ftp server. after the upoad the script deletes the file on my local machine (I'm not deleting it on the ftp).
try:
ftp = FTP(ftp.host.com)
ftp.login(your_username, your_password)
file = open(myfile.txt, "rb")
ftp.storbinary('STOR myfile.txt', file)
print 'STORing File now...'
ftp.quit()
file.close()
subprocess.Popen('del myfile.txt', shell=True)
print 'File deleted'
except all_errors:
print 'An error occured'
This code runs, however it's not reliable! At every ~10th upload my script hangs while STORing the file.
print 'STORing File now...' # So I just get 'STORING File now...'
The file is not big and should be uploaded within a few seconds, but I often have to wait an hour or two and only then the exception is thrown:
print 'An error occured'
If the exception were thrown 'earlier' it would be nice so I could just restart the upload (e.g. in a while loop). Because I need this file to be uploaded as soon as possible I need to make the file upload faster (I don't want to wait so long for the exception being thrown)
Second issue: Sometimes this happens: After the file has been successfully uploaded, the script fails to delete the file on my local machine because 'some other process is accessing it already' <- I think ftplib did not 'released' the file. What can I do to prevent this?
I'm searching for a better/reliable simple fileupload solution. Anyone have an idea?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要使用 subprocess 来删除文件 - os.unlink 调用将允许您以可移植的方式执行此操作(当 os 失败时,shutil 库会填补空白)
现在,您正在用愚蠢的 print 语句吞噬错误- 从异常中获取回溯,这将为您提供大量线索。但是,您的问题可能与套接字超时问题有关 - 您没有运行被动 FTP,或者服务器配置错误并给您提供了无效的被动连接端口号(防火墙阻止的内容)。
Don't use subprocess to shell out to delete a file - the os.unlink call will allow you to do this portably (the shutil library fills in the gaps when os fails)
Right now, you are gobbling the error with your silly print statement - get a traceback from the exception which would give you a large number of clues. However, your problem is likely related to socket timeout issues - either you are not running passive FTP, or the server is misconfigured and given you an invalid passive connect port number (something its firewall blocks).
怎么样,
记住,Python 有它自己的文件删除模块。不必要时不要调用 system del
how about this
remember, Python has its own file removing module. don't call system del unnecessarily
要更早地获取异常,请使用 socket.setdefaulttimeout :例如,
如果某个套接字阻塞了 20 秒,则会出现异常。
要从 Pyton 脚本中删除文件,请使用 os. unlink——比为
del
处理一个单独的进程要好得多。To get the exception earlier, use socket.setdefaulttimeout: e.g.,
will give you an exception if a socket it blocked for 20 seconds.
To remove a file from your Pyton script, use os.unlink -- much better than shelling out to a separate process for a
del
.