gpt4 book ai didi

Python 显式 FTP 不协商 TLS

转载 作者:太空宇宙 更新时间:2023-11-03 13:44:57 24 4
gpt4 key购买 nike

我正在尝试通过 FTP TLS 显式将文件传输到主机,但不知道如何处理证书。事务的 WinSCP 日志显示 TLS 协商已处理且证书已验证。但是我的 Python 脚本无法做到这一点。我知道证书指纹和密码,但不知道如何实现。

我的脚本:

import ftplib
import ssl


def main():
ctx = ssl._create_stdlib_context(ssl.PROTOCOL_TLSv1_2)
ftps = ftplib.FTP_TLS(context=ctx)

try:
ftps.set_debuglevel(2)
print(ftps.connect(host))
ftps.auth()
print(ftps.login(username,password))
print("1")
ftps.prot_p()
print("2")
#ftps.cwd('/')
print("3")
print (ftps.retrlines('LIST'))
print("4")
ftps.storbinary("STOR test.csv", open('C:\\test.csv', 'rb'))
print("5")
except Exception as ex:
print("error: ")
print(ex)

ftps.close()

input('Hit <ENTER> to close...')


if __name__ == "__main__":
main()

这是我的输出,最后一行是 Python 错误:

*get* '220 Microsoft FTP Service\n'
*resp* '220 Microsoft FTP Service'
220 Microsoft FTP Service
*cmd* 'AUTH TLS'
*put* 'AUTH TLS\r\n'
*get* '234 AUTH command ok. Expecting TLS Negotiation.\n'
*resp* '234 AUTH command ok. Expecting TLS Negotiation.'
*cmd* 'USER xxx'
*put* 'USER xxx\r\n'
*get* '331 Password required\n'
*resp* '331 Password required'
*cmd* 'PASS ********'
*put* 'PASS ********\r\n'
*get* '230 User logged in.\n'
*resp* '230 User logged in.'
230 User logged in.
1
*cmd* 'PBSZ 0'
*put* 'PBSZ 0\r\n'
*get* '200 PBSZ command successful.\n'
*resp* '200 PBSZ command successful.'
*cmd* 'PROT P'
*put* 'PROT P\r\n'
*get* '200 PROT command successful.\n'
*resp* '200 PROT command successful.'
2
3
*cmd* 'TYPE A'
*put* 'TYPE A\r\n'
*get* '200 Type set to A.\n'
*resp* '200 Type set to A.'
*cmd* 'PASV'
*put* 'PASV\r\n'
*get* '227 Entering Passive Mode (x,x,x,x,4,4).\n'
*resp* '227 Entering Passive Mode (x,x,x,x,4,4).'
*cmd* 'LIST'
*put* 'LIST\r\n'
*get* '125 Data connection already open; Transfer starting.\n'
*resp* '125 Data connection already open; Transfer starting.'
*get* '226 Transfer complete.\n'
*resp* '226 Transfer complete.'
226 Transfer complete.
4
*cmd* 'TYPE I'
*put* 'TYPE I\r\n'
*get* '200 Type set to I.\n'
*resp* '200 Type set to I.'
*cmd* 'PASV'
*put* 'PASV\r\n'
*get* '227 Entering Passive Mode (x,x,x,x,4,3).\n'
*resp* '227 Entering Passive Mode (x,x,x,x,4,3).'
*cmd* 'STOR test.csv'
*put* 'STOR test.csv\r\n'
*get* '125 Data connection already open; Transfer starting.\n'
*resp* '125 Data connection already open; Transfer starting.'
error:
The read operation timed out

最佳答案

A WinSCP log of the transaction shows that the TLS negotiation is handled and the certificate is verified. But my Python script fails to do this. I know the certificate fingerprint and the cipher, but don't know how to implement it.

您的 Python 脚本不会像您假设的那样在 TLS 协商中失败。

客户端使用 AUTH TLS 命令请求从普通连接升级到 TLS,该命令被服务器接受,可以在日志中看到:

*put* 'AUTH TLS\r\n'
*get* '234 AUTH command ok. Expecting TLS Negotiation.\n'

之后 TLS 协商(即 TLS 握手)完成。如果握手失败,客户端将中止。但是握手成功,因此客户端可以继续执行更多命令,这些命令也被服务器接受:

*put* 'USER xxx\r\n'
*get* '331 Password required\n'

因此,TLS 协商没有问题。数据传输本身也不是问题,可以看出客户端成功从服务器传输数据,即目录列表:

*put* 'PASV\r\n'
*get* '227 Entering Passive Mode (x,x,x,x,4,4).\n'
*put* 'LIST\r\n'
*get* '125 Data connection already open; Transfer starting.\n'
*get* '226 Transfer complete.\n'

只有在向服务器传输文件时才会失败,因为服务器没有在预期时间内回复:

*put* 'STOR test.csv\r\n'
*get* '125 Data connection already open; Transfer starting.\n'
error:
The read operation timed out

目前还不清楚服务器无法响应的原因。一个可能的原因可能是文件需要由某些应用程序(如防病毒软件)处理,然后服务器才能成功响应,并且此处理时间太长。

关于Python 显式 FTP 不协商 TLS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45865321/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com