简体   繁体   中英

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 . 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. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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