如何使用ftplib修复Python3中的连接重置错误,尝试执行ftp操作

时间:2019-06-28 14:57:31

标签: python-3.x ftp ftplib

当我手动尝试使用具有相同凭据的ftp时,我可以成功复制文件。但是,当尝试使用任何ftplib FTP命令时,出现连接重置错误...任何人都知道为什么?过程如下。

Python代码:

from ftplib import FTP
ftp = FTP('10.10.10.10')
ftp.set_debuglevel(5)
ftp.set_pasv('false') #false by default
ftp.login('user','pass')
ftp.dir()
ftp.close()

调试级别为5的日志:

python3 ftp.py
*get* '230 Login successful.\n'
*resp* '230 Login successful.'
*cmd* 'TYPE A'
*put* 'TYPE A\r\n'
*get* '200 Switching to ASCII mode.\n'
*resp* '200 Switching to ASCII mode.'
*cmd* 'PASV'
*put* 'PASV\r\n'
Traceback (most recent call last):
  File "ftp.py", line 8, in <module>
    ftp.dir()
  File "C:\Users\\AppData\Local\Programs\Python\Python37\lib\ftplib.py", line 575, in dir
    self.retrlines(cmd, func)
  File "C:\Users\\AppData\Local\Programs\Python\Python37\lib\ftplib.py", line 468, in retrlines
    with self.transfercmd(cmd) as conn, \
  File "C:\Users\\AppData\Local\Programs\Python\Python37\lib\ftplib.py", line 399, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
  File "C:\Users\\AppData\Local\Programs\Python\Python37\lib\ftplib.py", line 359, in ntransfercmd
    host, port = self.makepasv()
  File "C:\Users\\AppData\Local\Programs\Python\Python37\lib\ftplib.py", line 337, in makepasv
    host, port = parse227(self.sendcmd('PASV'))
  File "C:\Users\\AppData\Local\Programs\Python\Python37\lib\ftplib.py", line 273, in sendcmd
    return self.getresp()
  File "C:\Users\\AppData\Local\Programs\Python\Python37\lib\ftplib.py", line 236, in getresp
    resp = self.getmultiline()
  File "C:\Users\\AppData\Local\Programs\Python\Python37\lib\ftplib.py", line 222, in getmultiline
    line = self.getline()
  File "C:\Users\\AppData\Local\Programs\Python\Python37\lib\ftplib.py", line 204, in getline
    line = self.file.readline(self.maxline + 1)
  File "C:\Users\\AppData\Local\Programs\Python\Python37\lib\socket.py", line 589, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

下面是Windows ftp输出。

ftp -d -n 10.10.10.10
Connected to 10.10.10.10.
220 (vsFTPd 2.2.2)
---> OPTS UTF8 ON
200 Always in UTF8 mode.
ftp> user user
---> USER user
331 Please specify the password.
Password:
---> PASS pass
230 Login successful.
ftp> dir
---> PORT 172,24,198,16,5,52
200 PORT command successful. Consider using PASV.
---> LIST
150 Here comes the directory listing.
drwx--x--x    2 502      503         26624 Jul 01 17:00 dir1
drwx--x--x    2 502      503          6144 Jul 01 12:00 dir2
drwxr-xr-x    2 502      503          6144 Jul 02  2018 dir3
226 Directory send OK.
ftp: 205 bytes received in 0.01Seconds 17.08Kbytes/sec.
ftp> get dir1/file
---> PORT 172,24,198,16,5,77
200 PORT command successful. Consider using PASV.
---> RETR dir1/file
150 Opening BINARY mode data connection for dir1/file (55106 bytes).
226 Transfer complete.
ftp: 55106 bytes received in 0.17Seconds 318.53Kbytes/sec.

0 个答案:

没有答案