简体   繁体   English

Python3.6,ftp_tls和会话重用

[英]Python3.6, ftp_tls and session reuse

So, I'm struggling trying to upload a file to a remote ftps server that requires session reuse. 因此,我正在努力尝试将文件上传到需要会话重用的远程ftps服务器。

I must specify that even if I can connect to the server without problems via Filezilla, I get the error "unknown server certificate". 我必须指定,即使我可以通过Filezilla毫无问题地连接到服务器,也会收到错误“未知服务器证书”。

This is my script: 这是我的脚本:

import ftplib
import ssl

USER = "username"
PASS = "password"
SERVER = "1.2.3.4"
PORT = 21
BINARY_STORE = True

filepath = '/path/to/myfile.txt'
filename = 'myfile.txt'

content = open(filepath, 'rb')
ftp = ftplib.FTP_TLS()
ftp.set_debuglevel(2)
print (ftp.connect(SERVER, PORT))
print (ftp.login(USER, PASS))
print (ftp.prot_p())
print (ftp.set_pasv(True))

print (ftp.cwd("testfolder"))
print (ftp.storbinary('STOR %s' % filename, content))
ftp.quit()

But I get this message: 450 TLS session of data connection has not resumed or the session does not match the control connection 但我收到此消息: 数据连接的450 TLS会话未恢复或该会话与控制连接不匹配

The file is created on server but it's empty (0 bytes size) I've read dozen posts and I clearly understood that I must implement session reuse on client side too - which is supported from Python 3.6 - but simply... I don't know how to do this, since I can't find any working example. 该文件是在服务器上创建的,但为空(0字节大小),我读过十几篇文章,并且我清楚地了解到,我也必须在客户端实现会话重用-Python 3.6支持该功能-但简单来说...不知道该怎么做,因为我找不到任何有效的示例。

Someone suggested to extend FTP_TLS class: 有人建议扩展FTP_TLS类:

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:
            session = self.sock.session
            if isinstance(self.sock, ssl.SSLSocket):
                    session = self.sock.session
            conn = self.context.wrap_socket(conn,
                                            server_hostname=self.host,
                                            session=session)  # this is the fix
        return conn, size
ftp = MyFTP_TLS()
...

But now the server response is: 'SSLSocket' object has no attribute 'session' 但是现在服务器响应是: 'SSLSocket'对象没有属性'session'

So I'm stuck. 所以我被卡住了。 What am I missing? 我想念什么?

Is this working for you? 这对你有用吗?

from ftplib import FTP_TLS

USER = "username"
PASS = "password"
SERVER = "1.2.3.4"
PORT = 21 

ftps = FTP_TLS(SERVER)
ftps.login(USER, PASS)         
ftps.prot_p()         
ftps.set_pasv(True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM