首页 > 编程问答 >使用Python连接ftps服务器时nlst返回错误

使用Python连接ftps服务器时nlst返回错误

时间:2024-07-23 15:49:01浏览次数:9  
标签:python python-3.x ftp ftplib ftps

我可以使用 Total Commander 登录 服务器:ftps://publishedprices.co.il 用户名:“奥舍拉德” 密码为空

并带有 lftp -u osherad: publishedprices.co.il

但是当我尝试登录并使用Python获取文件列表时 nlst 函数返回超时

代码:

from ftplib import FTP_TLS

ftp_server = "publishedprices.co.il"

username = 'osherad'
password = ""

ftps = FTP_TLS()
ftps.set_debuglevel(2)
ftps.connect(ftp_server,timeout=30)
print('connected')
ftps.login(username, password)
ftps.prot_p()

print('log in')

file_list = ftps.nlst()

调试打印:

*get* '220-Welcome to Public Published Prices Server\n'
*get* '220-            Created by NCR L.T.D\n'
*get* '220-\n'
*get* '220-\n'
*get* '220 ** The site is open! Have a good day.\n'
*resp* '220-Welcome to Public Published Prices Server\n220-            Created by NCR L.T.D\n220-\n220-\n220 ** The site is open! Have a good day.'
connected
*cmd* 'AUTH TLS'
*put* 'AUTH TLS\r\n'
*get* '234 Authentication method accepted\n'
*resp* '234 Authentication method accepted'
*cmd* 'USER osherad'
*put* 'USER osherad\r\n'
*get* '331 User osherad, password please\n'
*resp* '331 User osherad, password please'
*cmd* 'PASS '
*put* 'PASS \r\n'
*get* '230 Password Ok, User logged in\n'
*resp* '230 Password Ok, User logged in'
*cmd* 'PBSZ 0'
*put* 'PBSZ 0\r\n'
*get* '200 PBSZ=0\n'
*resp* '200 PBSZ=0'
*cmd* 'PROT P'
*put* 'PROT P\r\n'
*get* '200 PROT P OK, data channel will be secured\n'
*resp* '200 PROT P OK, data channel will be secured'
log in
*cmd* 'TYPE A'
*put* 'TYPE A\r\n'
*get* '200 Type ASCII\n'
*resp* '200 Type ASCII'
*cmd* 'PASV'
*put* 'PASV\r\n'
*get* '227 Entering Passive Mode (194,90,26,21,47,54)\n'
*resp* '227 Entering Passive Mode (194,90,26,21,47,54)'

异常:

Traceback (most recent call last):
  File "c:\project\test.py", line 18, in <module>
    file_list = ftps.nlst()
                ^^^^^^^^^^^
  File "C:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\ftplib.py", line 553, in nlst
    self.retrlines(cmd, files.append)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\ftplib.py", line 462, in retrlines
    with self.transfercmd(cmd) as conn, \
         ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\ftplib.py", line 393, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\ftplib.py", line 793, in ntransfercmd
    conn, size = super().ntransfercmd(cmd, rest)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\ftplib.py", line 354, in ntransfercmd
    conn = socket.create_connection((host, port), self.timeout,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\socket.py", line 851, in create_connection
    raise exceptions[0]
  File "C:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\socket.py", line 836, in create_connection
    sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

当我设置 ftps.set_pasv(False) 我得到

*get* '220-Welcome to Public Published Prices Server\n'                                                                                                           [27/11310]*get* '220-            Created by NCR L.T.D\n'
*get* '220-\n'
*get* '220-\n'
*get* '220 ** The site is open! Have a good day.\n'
*resp* '220-Welcome to Public Published Prices Server\n220-            Created by NCR L.T.D\n220-\n220-\n220 ** The site is open! Have a good day.'
connected
*cmd* 'AUTH TLS'
*put* 'AUTH TLS\r\n'
*get* '234 Authentication method accepted\n'
*resp* '234 Authentication method accepted'
*cmd* 'USER osherad'
*put* 'USER osherad\r\n'
*get* '331 User osherad, password please\n'
*resp* '331 User osherad, password please'
*cmd* 'PASS '
*put* 'PASS \r\n'
*get* '230 Password Ok, User logged in\n'
*resp* '230 Password Ok, User logged in'
*cmd* 'PBSZ 0'
*put* 'PBSZ 0\r\n'
*get* '200 PBSZ=0\n'
*resp* '200 PBSZ=0'
*cmd* 'PROT P'
*put* 'PROT P\r\n'
*get* '200 PROT P OK, data channel will be secured\n'
*resp* '200 PROT P OK, data channel will be secured'
log in
*cmd* 'TYPE A'
*put* 'TYPE A\r\n'
*get* '200 Type ASCII\n'
*resp* '200 Type ASCII'
*cmd* 'PORT 172,17,118,200,159,233'
*put* 'PORT 172,17,118,200,159,233\r\n'
*get* '500 Port command invalid\n'
*resp* '500 Port command invalid'

编辑: ltfp 日志:

lftp :~>debug -o log1.txt -c -t 9
lftp :~>set ftp:ssl-force true
lftp :~>set ssl:verify-certificate no
lftp :~>set ftp:use-feat false
lftp :~>connect -u osherad: publishedprices.co.il
lftp :~>ls 

日志文件:

2024-07-22 02:22:22 publishedprices.co.il ---- Resolving host address...
2024-07-22 02:22:22 publishedprices.co.il ---- IPv6 is not supported or configured
2024-07-22 02:22:22 publishedprices.co.il ---- 1 address found: 194.90.26.22
2024-07-22 02:22:26 publishedprices.co.il ---- Connecting to publishedprices.co.il (194.90.26.22) port 21
2024-07-22 02:22:26 publishedprices.co.il <--- 220-Welcome to Public Published Prices Server
2024-07-22 02:22:26 publishedprices.co.il <--- 220-            Created by NCR L.T.D
2024-07-22 02:22:26 publishedprices.co.il <--- 220-
2024-07-22 02:22:26 publishedprices.co.il <--- 220-
2024-07-22 02:22:26 publishedprices.co.il <--- 220 ** The site is open! Have a good day.
2024-07-22 02:22:26 publishedprices.co.il ---> AUTH TLS
2024-07-22 02:22:26 publishedprices.co.il <--- 234 Authentication method accepted
2024-07-22 02:22:26 publishedprices.co.il ---> USER osherad
2024-07-22 02:22:26 Certificate: CN=*.publishedprices.co.il
2024-07-22 02:22:26  Issued by:        C=GB,ST=Greater Manchester,L=Salford,O=Sectigo Limited,CN=Sectigo RSA Domain Validation Secure Server CA
2024-07-22 02:22:26  Checking against: C=GB,ST=Greater Manchester,L=Salford,O=Sectigo Limited,CN=Sectigo RSA Domain Validation Secure Server CA
2024-07-22 02:22:26   Trusted
2024-07-22 02:22:26 Certificate: C=GB,ST=Greater Manchester,L=Salford,O=Sectigo Limited,CN=Sectigo RSA Domain Validation Secure Server CA
2024-07-22 02:22:26  Issued by:        C=US,ST=New Jersey,L=Jersey City,O=The USERTRUST Network,CN=USERTrust RSA Certification Authority
2024-07-22 02:22:26  Checking against: C=US,ST=New Jersey,L=Jersey City,O=The USERTRUST Network,CN=USERTrust RSA Certification Authority
2024-07-22 02:22:26   Trusted
2024-07-22 02:22:26 Certificate: C=US,ST=New Jersey,L=Jersey City,O=The USERTRUST Network,CN=USERTrust RSA Certification Authority
2024-07-22 02:22:26  Issued by: C=GB,ST=Greater Manchester,L=Salford,O=Comodo CA Limited,CN=AAA Certificate Services
2024-07-22 02:22:26   Trusted
2024-07-22 02:22:26 publishedprices.co.il <--- 331 User osherad, password please
2024-07-22 02:22:26 publishedprices.co.il ---> PASS 
2024-07-22 02:22:26 publishedprices.co.il <--- 230 Password Ok, User logged in
2024-07-22 02:22:26 publishedprices.co.il ---> PWD
2024-07-22 02:22:26 publishedprices.co.il <--- 257 "/" is the current directory
2024-07-22 02:22:26 publishedprices.co.il ---> PBSZ 0
2024-07-22 02:22:26 publishedprices.co.il <--- 200 PBSZ=0
2024-07-22 02:22:26 publishedprices.co.il ---> PROT P
2024-07-22 02:22:26 publishedprices.co.il <--- 200 PROT P OK, data channel will be secured
2024-07-22 02:22:26 publishedprices.co.il ---> PASV
2024-07-22 02:22:26 publishedprices.co.il <--- 227 Entering Passive Mode (194,90,26,21,48,206)
2024-07-22 02:22:26 publishedprices.co.il ---- Connecting data socket to (194.90.26.21) port 12494
2024-07-22 02:22:26 publishedprices.co.il ---- Data connection established
2024-07-22 02:22:26 publishedprices.co.il ---> LIST
2024-07-22 02:22:26 publishedprices.co.il <--- 150 Opening data connection
2024-07-22 02:22:26 Certificate: CN=*.publishedprices.co.il
2024-07-22 02:22:26  Issued by:        C=GB,ST=Greater Manchester,L=Salford,O=Sectigo Limited,CN=Sectigo RSA Domain Validation Secure Server CA
2024-07-22 02:22:26  Checking against: C=GB,ST=Greater Manchester,L=Salford,O=Sectigo Limited,CN=Sectigo RSA Domain Validation Secure Server CA
2024-07-22 02:22:26   Trusted
2024-07-22 02:22:26 Certificate: C=GB,ST=Greater Manchester,L=Salford,O=Sectigo Limited,CN=Sectigo RSA Domain Validation Secure Server CA
2024-07-22 02:22:26  Issued by:        C=US,ST=New Jersey,L=Jersey City,O=The USERTRUST Network,CN=USERTrust RSA Certification Authority
2024-07-22 02:22:26  Checking against: C=US,ST=New Jersey,L=Jersey City,O=The USERTRUST Network,CN=USERTrust RSA Certification Authority
2024-07-22 02:22:26   Trusted
2024-07-22 02:22:26 Certificate: C=US,ST=New Jersey,L=Jersey City,O=The USERTRUST Network,CN=USERTrust RSA Certification Authority
2024-07-22 02:22:26  Issued by: C=GB,ST=Greater Manchester,L=Salford,O=Comodo CA Limited,CN=AAA Certificate Services
2024-07-22 02:22:26   Trusted
2024-07-22 02:22:26 publishedprices.co.il <--- 226 Transfer complete
2024-07-22 02:22:26 publishedprices.co.il ---- Got EOF on data connection
2024-07-22 02:22:26 publishedprices.co.il ---- Closing data socket
2024-07-22 02:22:29 publishedprices.co.il ---> QUIT
2024-07-22 02:22:29 publishedprices.co.il <--- 221 Goodbye
2024-07-22 02:22:29 publishedprices.co.il ---- Closing control socket

问题在于该 FTPS 服务器使用的是罕见的非标准扩展被动模式 (EPSV),而 ftplib 不支持该模式。解决此问题的方案是使用支持 EPSV 的 FTPS 库,例如 tlsftp

以下是用 tlsftp 连接到的 FTPS 服务器并获取文件列表的 Python 代码示例:

from tlsftp import TLSFTP

ftp_server = "publishedprices.co.il"
username = 'osherad'
password = ""

with TLSFTP(ftp_server, username, password) as ftps:
    ftps.prot_p()
    file_list = ftps.nlst()
    print(file_list)

代码说明:

  1. 导入 TLSFTP 类: tlsftp 库中导入 TLSFTP 类。
  2. 创建 TLSFTP 对象: 使用服务器地址、用户名和密码创建 TLSFTP 对象。建议使用 with 语句来自动关闭连接。
  3. 启用数据通道加密: 使用 prot_p() 方法启用数据通道加密。
  4. 获取文件列表: 使用 nlst() 方法获取文件列表。
  5. 打印文件列表: 将获取到的文件列表打印到控制台。

安装 tlsftp 库:

可以使用 pip 安装 tlsftp 库:

pip install tlsftp

其他注意事项:

  • 确保的 Python 版本支持 tlsftp 库。
  • 如果在连接到服务器时遇到问题,请尝试在 TLSFTP 对象上设置 debug=True 以查看详细的调试信息。

通过使用 tlsftp 库,应该能够成功连接到使用 EPSV 的 FTPS 服务器并获取文件列表。

标签:python,python-3.x,ftp,ftplib,ftps
From: 78776035

相关文章

  • 在我的 python 代码中,我无法使用 slixmpp 连接到 Openfire 服务器
    在我的python代码中,我无法连接到Openfire服务器。我想连接到OpenFire服务器我的代码是importthreadingimporttkinterastkfromtkinterimportsimpledialog,scrolledtextimportjsonimportloggingimportasynciofromslixmppimportClientXMPPcla......
  • 如何在Python中获取字符的unicode?
    想要获取中文或越南语的汉名和日文字符的unicode我已经尝试过这些代码text="......
  • 三种语言实现计算逆序对的数量(C++/Python/Java)
    题目给定一个长度为......
  • python项目中__init__.py是什么文件有什么作用
    __init__.py 文件在Python项目中的作用主要是将目录标识为一个Python包。它在包的初始化过程中起到了重要作用。具体来说,__init__.py 文件有以下几个功能:1.标识包:-当一个目录中包含 __init__.py 文件时,这个目录就被视为一个Python包。这样,Python解释器就能够识别......
  • 从零开始构建你的第一个Python Web应用
    在本文中,我们将带领你从零开始构建一个简单的PythonWeb应用。不需要任何先验知识,我们会一步步地指导你完成设置、框架选择、代码编写到部署的整个过程。无论你是Web开发新手还是希望扩展技能的老手,这篇文章都将为你提供一个实践操作的起点。一、前言随着互联网技术的飞速......
  • Python中的global关键字是如何工作的?
    在Python中,global关键字扮演着特殊而重要的角色,它主要用于在函数内部声明全局变量。理解global关键字的工作原理,首先需要明确Python中变量作用域的概念,以及为什么需要global关键字。下面,我将详细解释global关键字的工作机制,包括其作用、使用场景、注意事项,以及它在Python编......
  • Python中enumerate()函数
    Python中enumerate()函数的作用是什么?enumerate()函数在Python中是一个非常有用的内置函数,它用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在for循环当中。使用enumerate()可以使你在循环中获取每个元素的同时,也能......
  • 我无法解决 python 捕获脚本中的此错误
    defclick_folder(self,文件夹名称):尝试:#单击主文件夹main_folder=self.wait.until(EC.presence_of_element_located((By.XPATH,f"//div[contains(@class,'folders-text_block')]//div[text()='{folder_name}']")))main_folder.click()print......
  • 尝试从图像 url 中抓取图像(使用 python urllib ),但获取 html
    我尝试从以下网址获取图像。http://upic.me/i/fj/the_wonderful_mist_once_again_01.jpg我可以右键单击并另存为,但是当我尝试使用urlretrievelikeimporturllibimg_url='http://upic.me/i/fj/the_wonderful_mist_once_again_01.jpg'urllib.urlretriev......
  • Python-深度学习算法实用指南-全-
    Python深度学习算法实用指南(全)原文:zh.annas-archive.org/md5/844a6ce45a119d3197c33a6b5db2d7b1译者:飞龙协议:CCBY-NC-SA4.0前言深度学习是人工智能领域最受欢迎的领域之一,允许你开发复杂程度各异的多层模型。本书介绍了从基础到高级的流行深度学习算法,并展示了如何使用......