
import os
from ftplib import FTP
from io import StringIO
import sys
if len(sys.argv) >= 4:
print("第一个参数:",sys.argv[1])
print("第二个参数:",sys.argv[2])
print("第三个参数:",sys.argv[3])
else:
print("没有提供足够的参数!")
def is_ftp_connection_successful(host, username, password):
try:
myFtpObject = FTP(host)
myFtpObject.login(user = username, passwd = password)
output = StringIO()
original_stdout = sys.stdout
sys.stdout = output
myFtpObject.dir()
sys.stdout = original_stdout
print("FTP连接成功。")
return True
except Exception is e:
print(f"FTP连接失败:{e}")
return False
finally:
if 'myFtpObject' in locals():
myFtpObject.quit()
host = sys.argv[1]
username = sys.argv[2]
password = sys.argv[3]
if is_ftp_connection_successful(host, username, password):
print("可以进行后续操作!")
else:
print("FTP服务器连接失败!")
数空格数,数的有点累啊

H:\python\studyPython>python ftpConnTestClass.py 192.168.102.234 stFtpCommUser stFtpComm@User
File "H:\python\studyPython\ftpConnTestClass.py", line 17
sys.stdout = output
^
TabError: inconsistent use of tabs and spaces in indentation
H:\python\studyPython>python ftpConnTestClass.py 192.168.102.234 stFtpCommUser stFtpComm@User
File "H:\python\studyPython\ftpConnTestClass.py", line 17
sys.stdout = output
^
TabError: inconsistent use of tabs and spaces in indentation
H:\python\studyPython>python ftpConnTestClass.py 192.168.102.234 stFtpCommUser stFtpComm@User
File "H:\python\studyPython\ftpConnTestClass.py", line 18
myFtpObject.dir()
^
TabError: inconsistent use of tabs and spaces in indentation
H:\python\studyPython>python ftpConnTestClass.py 192.168.102.234 stFtpCommUser stFtpComm@User
第一个参数: 192.168.55.66
第二个参数: ftpTest
第三个参数: abc
FTP连接成功。
可以进行后续操作!