gpt4 book ai didi

python - 通过 FTP_TLS 上传文件到 FTP 服务器得到错误 ssl

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

我使用 filezilla 服务器来创建 ftp 服务器。 ssl 选项在端口 21 和 990 上打开。我尝试使用 filezilla 客户端上传和下载文件仍然运行良好。我尝试使用 ftplib 将文件上传到 FTP 服务器。这是我的代码:

from ftplib import FTP_TLS, FTP
import socket
import ssl

file = open('test.py','rb')

ftps = FTP_TLS()
ftps.set_debuglevel(2)
s = ftps.connect(host='192.168.1.102', port=21)
ftps.login(user="xxx", passwd="xxxxxx")
ftps.prot_p()
ftps.storbinary("STOR test.py", file)

并得到错误:

*get* '227 Entering Passive Mode (192,168,1,102,19,170)\r\n'
*resp* '227 Entering Passive Mode (192,168,1,102,19,170)'
*cmd* 'STOR test.py'
*put* 'STOR test.py\r\n'
*get* '150 Opening data channel for file upload to server of "/test.py"\r\n'
*resp* '150 Opening data channel for file upload to server of "/test.py"'
Traceback (most recent call last):
File "ImplicitFTP.py", line 49, in <module>
ftps.storbinary("STOR test.py", file)
File "C:\Python27\lib\ftplib.py", line 760, in storbinary
conn = self.transfercmd(cmd, rest)
File "C:\Python27\lib\ftplib.py", line 376, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "C:\Python27\lib\ftplib.py", line 713, in ntransfercmd
server_hostname=self.host)
File "C:\Python27\lib\ssl.py", line 363, in wrap_socket
_context=self)
File "C:\Python27\lib\ssl.py", line 611, in __init__
self.do_handshake()
File "C:\Python27\lib\ssl.py", line 840, in do_handshake
self._sslobj.do_handshake()
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:661)

请帮帮我。谢谢

最佳答案

问题可能是 FTP 服务器要求新数据通道中的 TLS session 与控制 channel 相同。这在 Python 3.7 中尚未修复。子类 ftplib.FTP_TLS 在这里找到的解决方案 https://stackoverflow.com/a/43301750我做了一个小修复:

import ftplib


class ReusedSslSocket(SSLSocket):
def unwrap(self):
pass


class MyFTP_TLS(ftplib.FTP_TLS):
"""Explicit FTPS, with shared TLS session"""
def ntransfercmd(self, cmd, rest=None):
conn, size = ftplib.FTP.ntransfercmd(self, cmd, rest)
if self._prot_p:
conn = self.context.wrap_socket(conn,
server_hostname=self.host,
session=self.sock.session) # reuses TLS session
conn.__class__ = ReusedSslSocket # we should not close reused ssl socket when file transfers finish
return conn, size

像这样使用它:

ftps = MyFTP_TLS()

关于python - 通过 FTP_TLS 上传文件到 FTP 服务器得到错误 ssl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47626476/

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