gpt4 book ai didi

python - 当尝试使用 Python FTP_TLS 对象下载文件时出现 AttributeError?

转载 作者:行者123 更新时间:2023-11-30 22:39:08 25 4
gpt4 key购买 nike

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)

我已经从这篇文章 Python FTP implicit TLS connection issue 复制了一些代码。我对代码中发生的所有事情都有很好的理解,但我完全迷失了这个错误。问题是当我调用 retrbinary 函数时,我能够运行所有内容直到最后一行。我收到错误:

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

完整的错误对话是:

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'

有人知道罪魁祸首可能是什么吗?

最佳答案

您对超时参数(它是一个 int)的使用位于此行中上下文变量的位置:

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

应该是:

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

参见: https://docs.python.org/2/library/ftplib.html

关于python - 当尝试使用 Python FTP_TLS 对象下载文件时出现 AttributeError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43267410/

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