[英]Handling FTP errors via subprocess in Python
I want to handle FTP error codes ran in a subprocess that is run as part of this python script so in essence if the FTP command returns code 550 file not found I want to somehow handle this exception in python but I'm not 100% sure how to do this any direction would be appreciated! 我想处理在作为此python脚本的一部分运行的子进程中运行的FTP错误代码,因此,实质上,如果FTP命令返回未找到代码550文件,我想以某种方式在python中处理此异常,但我不确定100%如何做到这一点任何方向将不胜感激!
#!/usr/bin/python
import argparse
import ftplib
import os
import subprocess
import sys
parser = argparse.ArgumentParser()
parser.add_argument('--ftp', help='FTP address to retrieve DA from', dest='ftpconn', required=True)
parser.add_argument('--user', help='FTP username', dest='user', required=True)
parser.add_argument('--password', help='FTP password', dest='passwd')
parser.add_argument('--file', help='File(s) to retrieve', dest='file', nargs='+')
parser.add_argument('-d', '--destination', help='Destination folder for transferred files, if left blank default directory will be the working directory', default='./', dest='dfolder')
args = parser.parse_args()
command = """user %s %s
cd ..
lcd %s
get %s
bye"""
for idx, item in enumerate(args.file):
pobj = subprocess.Popen(['ftp', '-nv', args.ftpconn], stdin=subprocess.PIPE, stderr=subprocess.PIPE)
pobj.communicate(command % (args.user, args.passwd, args.dfolder, args.file[idx]))
subprocess
object returns the output and error subprocess
对象返回输出和错误
sStdout, sStdErr = probj.communicate(your_command)
More over you can get the return code of the process object by: 您还可以通过以下方法获得流程对象的返回码:
nRetCode = probj.returncode
A None value indicates that the process hasn't terminated yet. None值表示该进程尚未终止。
A negative value -N indicates that the child was terminated by signal N (Unix only). 负值-N表示该子级已由信号N终止(仅Unix)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.