[英]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.