简体   繁体   English

尝试使用Python FTP_TLS对象下载文件时获取AttributeError?

[英]When trying to download files using a Python FTP_TLS object getting AttributeError?

from ftplib import FTP_TLS
import socket
import ssl

class tyFTP(FTP_TLS):
    def __init__(self, host='', user='', passwd='', acct='', keyfile=None, certfile=None, timeout=60):
        FTP_TLS.__init__(self, host, user, passwd, acct, keyfile, certfile, timeout)
    def connect(self, host='', port=0, timeout=-999):
        if host != '':
            self.host = host
        if port > 0:
            self.port = port
        if timeout != -999:
            self.timeout = timeout

        try: 
            self.sock = socket.create_connection((self.host, self.port), self.timeout)
            self.af = self.sock.family
            self.sock = ssl.wrap_socket(self.sock, self.keyfile, self.certfile, ssl_version=ssl.PROTOCOL_TLSv1)
            self.file = self.sock.makefile('rb')
            self.welcome = self.getresp()
        except Exception as e:
            print e
        return self.welcome

# FTP_ROOT_PATH = "/outgoing/"
FTP_SITE = "..."
# FTP_SITE = "..."
FTP_PORT = 990

UPLOAD = {
    "USERNAME": "...",
    "PASSWORD": "..."
}

DOWNLOAD = {
    "USERNAME": "...",
    "PASSWORD": "..."
}

remote_file = "..."
local_filepath = "..."

server = tyFTP()
server.connect(host=FTP_SITE, port=990)
server.login(user=DOWNLOAD['USERNAME'], passwd=DOWNLOAD['PASSWORD'])
server.prot_p()
server.retrbinary("RETR " + remote_file, open(local_filepath, "wb").write)

I've copied some code already from this post Python FTP implicit TLS connection issue . 我已经从这篇Python FTP隐式TLS连接问题中复制了一些代码。 I have a good understanding of everything that is going on in the code but am completely lost with the error. 我对代码中发生的所有事情都有很好的理解,但完全没有错误。 The issue is I am able to run everything up until the last line when I'm calling the retrbinary function. 问题是,当我调用retrbinary函数时,我可以运行所有操作,直到最后一行。 I am getting the error: 我收到错误:

AttributeError: 'int' object has no attribute 'wrap_socket'

The full error dialogue is: 完整的错误对话是:

File "sample.py", line 48, in <module>
server.retrbinary("RETR " + remote_file, open(local_filepath, "wb").write)
File "C:\Users\Alex\Anaconda2\lib\ftplib.py", line 718, in retrbinary
conn = self.transfercmd(cmd, rest)
File "C:\Users\Alex\Anaconda2\lib\ftplib.py", line 376, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "C:\Users\Alex\Anaconda2\lib\ftplib.py", line 712, in ntransfercmd
conn = self.context.wrap_socket(conn,
AttributeError: 'int' object has no attribute 'wrap_socket'

Does anyone have any insight on what the culprit might be? 有没有人对罪魁祸首是什么有任何见解?

Your use of the timeout arg (which is an int) is in the PLACE of the context variable in this line: 您对超时arg(这是一个int)的使用位于此行中上下文变量的PLACE中:

FTP_TLS.__init__(self, host, user, passwd, acct, keyfile, certfile, timeout)

Should be: 应该:

FTP_TLS.__init__(self, host, user, passwd, acct, keyfile, certfile, context, timeout)

See: https://docs.python.org/2/library/ftplib.html 请参阅: https//docs.python.org/2/library/ftplib.html

暂无
暂无

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

相关问题 尝试使用 FTP_TLS 从同一目录下载第二个文件时出现“ftplib.error_perm: 550 Operation not allowed” - "ftplib.error_perm: 550 Operation not permitted" when trying to download the second file from the same directory using FTP_TLS Python:nlst()使用FTP_TLS挂在“清单上来了” - Python: nlst() hangs on “here come the listing” by using FTP_TLS 当密码包含时,FTP_TLS 530 使用 Python3 登录不正确 § - FTP_TLS 530 Login Incorrect with Python3 when password contains § Python3.6,ftp_tls和会话重用 - Python3.6, ftp_tls and session reuse Python FTPS (FTP_TLS) 连接被拒绝。 没有证书? - Python FTPS (FTP_TLS) connection refused. No certificate? Python 模块 ftplib FTP_TLS - 错误 530 - Python module ftplib FTP_TLS - Error 530 无法在EC2上导入FTP_TLS - Cannot import FTP_TLS on EC2 如何在 urllib.request.urlopen() 中使用 FTP_TLS(显式模式)(或等效于 FTP_TLS 的 `urlopen`) - How to use FTP_TLS (Explicit mode) with urllib.request.urlopen() (or equivalent of `urlopen` with FTP_TLS) 尝试使用 Jira Python API 获取数据时出现错误 [AttributeError: &#39;bool&#39; object has no attribute &#39;error&#39;] - Getting error [ AttributeError: 'bool' object has no attribute 'error' ] when trying to get data using Jira Python API Python Seaborn:获取AttributeError:尝试绘图时&#39;str&#39;对象没有属性&#39;get&#39; - Python Seaborn: getting AttributeError: 'str' object has no attribute 'get' when trying to plot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM