【问题标题】:Python ftplib msld error: 500 Command not understoodPython ftplib msld 错误:500 命令不理解
【发布时间】:2018-06-14 16:34:49
【问题描述】:

我正在尝试创建一个用于从 FTP 获取文件的 Python 脚本。它现在正在运行,但我在免费托管页面https://hosting.miarroba.com 上对其进行了测试。如果我将它与我的专用服务器 FTP 服务连接,它会失败并显示

500 命令不理解

我认为失败的命令是ftp.mlsd()。我不知道我是否需要在我的服务器上进行特殊配置。

编码 Python 3.5

def realizarConexion():
a_server = ""
a_user = ""
a_pass = ""
a_port = 

# Conectarse con los métodos connect y login
try:
    ftp = FTP() 
    ftp.connect(a_server, a_port, -999) 
    ftp.login(a_user, a_pass)
    #ftp.dir()
    estado = validaFecha()
    if estado:
        descargarFicheros(ftp)
    else:
        print("No existe una anterior fecha de respaldo configurada")
    ftp.close()
except Exception as e:
    print("Fallo al conectar con FTP %s: %s" %(a_server, e))

def obtenerFecha():
ultFecha = ""
try:
    fDate = open("date.txt","r")
    if fDate.mode == 'r':
        ultFecha = fDate.read()
except Exception as e:
    print("Sin fecha asignada!")
return ultFecha

def validaFecha():
estadoFecha = True
try:
    fDate = open("date.txt","r")
    if fDate.mode == 'r':
        ultFecha = fDate.read()
        print("fecha: %s" %ultFecha)
except Exception as e:
    respaldo = input('No hay fechas configuradas, desea tomar la fecha actual como fecha de ultimo respaldo! - (Y / N): ')
    if respaldo.lower() == "y":
       asignarFechaCopia()
       print("La asignación de la fecha se a ha generado con exito")
       estadoFecha = True
    elif respaldo.lower()  == "n":
        print("Copia de seguridad detenida!") 
        estadoFecha = False
    else:
        print("Copia de seguridad detenida!")
        estadoFecha = False
return estadoFecha

def descargarFicheros(ftp): 
try:
    for file, parametros in ftp.mlsd():
        if file != '.' and file != '..':
            fechaCopia = obtenerFecha()[0:10]
            horaCopia = obtenerFecha()[11:19]
            fechaModif = datetime.datetime.strptime(parametros["modify"][0:-6],"%Y%m%d").date()
            horaModif = datetime.datetime.strptime(parametros["modify"][8:14],"%H%M%S").time()
            f1 = time.strptime(str(fechaCopia) + " " + str(horaCopia), "%Y-%m-%d %H:%M:%S")
            f2 = time.strptime(str(fechaModif) + " " + str(horaModif), "%Y-%m-%d %H:%M:%S")
            if f2 > f1:
                print("Se ha actualizado el fichero => %s" %file)
                ftp.retrbinary("RETR " + file ,open(file, 'wb').write)
            else:
                print("%s Archivo sin cambios %s %s " %(fechaCopia,fechaModif,file))
    print("Proceso finalizado!! Se ha actualzado la fecha de copiado de archivos.")
    asignarFechaCopia()                     
except Exception as e:
    print("Error: %s" %e)    

def asignarFechaCopia():
t = time.strftime("%Y-%m-%d %H:%M:%S")
fDate = open("date.txt","w+")
fDate.write(t)
fDate.close()

#Inicialización del Script     
realizarConexion()

【问题讨论】:

    标签: python windows iis ftp ftplib


    【解决方案1】:

    许多服务器不支持MLSD 命令,因为它是一个相对较新的命令(在 FTP 术语中)。尤其是 IIS 没有。


    如果您只需要文件名,请改用FTP.nlst

    如果您需要文件属性,则必须改用FTP.dir(或FTP.retrlines)并解析返回的列表。

    另见How do I parse a listing of files to get just the filenames in Python?

    【讨论】:

    • 感谢您的更正和回答!!,如果我需要文件时间戳,FTP.nlst 是否有效?
    • No nlst 不会给你时间戳(除非你为nlst 返回的每个文件调用MDTM,另见stackoverflow.com/q/29026709/850848)。对于不支持 MLSD 的服务器上带有时间戳的列表,您需要 dirLIST 命令)。
    【解决方案2】:

    您可以使用 wireshark 或 tcpdump 通过过滤端口 21 来查看以明文形式发送到服务器的内容。 您应该能够确切地看到发生了什么以及“500 命令不理解”的来源。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-06
      • 2015-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-12
      • 2012-11-12
      相关资源
      最近更新 更多