Skip to main content
 首页 » 编程设计

python之运行 FTP.retrbinary 检索文件时出错

2024年10月25日11sky-heaven

我正在使用 pythons ftplib 尝试从远程服务器检索文件(练习项目)。我在发送文件时没有遇到任何问题,但在尝试检索时遇到错误。我正在使用 python 3。这是我的代码:

from ftplib import FTP 
 
user = 'myusername' 
passw = 'mypassw' 
url = input('FTP url: ').lower().strip() 
ftp = FTP(url)      #connect to the host, default port 
ftp.login(user, passw) # enters login info 
 
def getfile(): 
    ftp.retrlines('LIST')   # List directory contents 
    filename = input('Name of file: ') 
    localfile = open(filename, 'wb') 
    ftp.retrbinary('RETR' + filename, localfile.write, 1024) 
    localfile.close() 
 
getfile() 

这是我得到的错误:

Traceback (most recent call last): 
  File "C:\Users\User\Desktop\School\python\ftpget.py", line 16, in <module> 
    getfile() 
  File "C:\Users\User\Desktop\School\python\ftpget.py", line 13, in getfile 
    ftp.retrbinary('RETR' + filename, localfile.write, 1024) 
  File "C:\Python34\lib\ftplib.py", line 441, in retrbinary 
    with self.transfercmd(cmd, rest) as conn: 
  File "C:\Python34\lib\ftplib.py", line 398, in transfercmd 
    return self.ntransfercmd(cmd, rest)[0] 
  File "C:\Python34\lib\ftplib.py", line 364, in ntransfercmd 
    resp = self.sendcmd(cmd) 
  File "C:\Python34\lib\ftplib.py", line 272, in sendcmd 
    return self.getresp() 
  File "C:\Python34\lib\ftplib.py", line 245, in getresp 
    raise error_perm(resp) 
ftplib.error_perm: 500 ? 

知道是什么导致了错误吗?

请您参考如下方法:

如果响应的状态代码以“5”开头,

ftplib 会引发错误。这意味着服务器正在返回 5xx 错误:

0.5.1 500 Internal Server Error

The server encountered an unexpected condition which prevented it from fulfilling the request.

10.5.2 501 Not Implemented

The server does not support the functionality required to fulfill the request. This is the appropriate response when the server does not recognize the request method and is not capable of supporting it for any resource.

10.5.3 502 Bad Gateway

The server, while acting as a gateway or proxy, received an invalid response from the upstream server it accessed in attempting to fulfill the request.

10.5.4 503 Service Unavailable

The server is currently unable to handle the request due to a temporary overloading or maintenance of the server. The implication is that this is a temporary condition which will be alleviated after some delay. If known, the length of the delay MAY be indicated in a Retry-After header. If no Retry-After is given, the client SHOULD handle the response as it would for a 500 response.

  Note: The existence of the 503 status code does not imply that a 
  server must use it when becoming overloaded. Some servers may wish 
  to simply refuse the connection. 

10.5.5 504 Gateway Timeout

The server, while acting as a gateway or proxy, did not receive a timely response from the upstream server specified by the URI (e.g. HTTP, FTP, LDAP) or some other auxiliary server (e.g. DNS) it needed to access in attempting to complete the request.

  Note: Note to implementors: some deployed proxies are known to 
  return 400 or 500 when DNS lookups time out. 

10.5.6 505 HTTP Version Not Supported

The server does not support, or refuses to support, the HTTP protocol version that was used in the request message. The server is indicating that it is unable or unwilling to complete the request using the same major version as the client, as described in section 3.1, other than with this error message. The response SHOULD contain an entity describing why that version is not supported and what other protocols are supported by that server.

根据ftp docs :

FTP.retrbinary(command, callback[, maxblocksize[, rest]])
Retrieve a file in binary transfer mode. command should be an appropriate RETR command: 'RETR filename'.

但是你写了这个:

ftp.retrbinary('RETR' + filename, localfile.write, 1024) 

这会产生这样的东西:

ftp.retrbinary('RETRdog.jpg', localfile.write, 1024) 

'RETR'后需要加一个空格。

用python创建你自己的ftp服务器

顺便说一下,您可以编写一个简短的 python 程序来充当 ftp 服务器。首先安装模块pyftpdlib:

$ pip3.4 install pyftpdlib  #See note on 3.4 below 

如果你的系统上只有一个版本的python,你可以这样写:

$ pip install pyftpdlib 

如果您的计算机上有多个版本的 python,请用正确的版本号替换 3.4。您指定的版本号是将安装 pyftpdlib 模块的 python 版本。

下面是一个 ftp 服务器在 python 中的样子:

from pyftpdlib.authorizers import DummyAuthorizer 
from pyftpdlib.handlers import FTPHandler 
from pyftpdlib.servers import FTPServer 
 
my_authorizer = DummyAuthorizer() 
my_authorizer.add_user( 
    username = '7stud',  
    password = '1234',  
    homedir = '.',  
    perm='elrw'  #permissions: 'e'->change dir, 'l'->list files, 'r'->retrieve files, 'w'->store a file to the server 
) 
 
my_handler = FTPHandler 
my_handler.authorizer = my_authorizer 
 
port = 2121 
my_handler.banner = "You connected to my ftp server on port: {}".format(port) 
 
address = ('localhost', port) 
server = FTPServer(address, my_handler) 
 
server.max_cons = 256 
server.max_cons_per_ip = 5 
 
server.serve_forever() 

pyftpdlib 文档和教程 here . add_user() 和权限描述 here .

然后,您可以为您的服务器创建一个目录:

$ mkdir ftp_server 
$ cd ftp_server 
$ touch my_ftp_server.py 

将上面的代码放入文件my_ftp_server.py。然后将一些文件添加到您要练习“下载”的 ftp_server 目录。然后通过运行 my_ftp_server.py 启动服务器。

在另一个终端窗口中,将目录切换到包含您的 python 程序的目录:

$ cd python_programs 

然后对您的 ftp 客户端程序做一些小改动:

from ftplib import FTP 
 
def getfile(conn):  #It's good practice not to refer to global variables in your functions. 
                    #Instead, pass in any values your function needs as arguments. 
    conn.retrlines('LIST')   # List directory contents 
 
    filename = input('Name of file: ') 
    localfile = open(filename, 'wb') 
    ftp_command = 'RETR {}'.format(filename) 
 
    conn.retrbinary(ftp_command, localfile.write, 1024) 
 
    localfile.close() 
 
user = '7stud' 
passw = '1234' 
port = 2121 
 
url = input('FTP url: ').lower().strip() 
 
ftp = FTP() 
ftp.connect(url, port) 
ftp.login(user, passw)  
getfile(ftp) 
ftp.quit() 

这是您的 ftp 客户端程序的示例运行:

~/python_programs$ python3.4 ftp_client.py  
FTP url: localhost 
 
drwxr-xr-x   4 7stud    staff         136 Feb 11 09:07 cgi-bin 
-rw-r--r--   1 7stud    staff        3446 Jun 08  2013 client_socket.py 
-rw-r--r--   1 7stud    staff         680 Feb 15 03:03 ftp_server.py 
-rw-r--r--   1 7stud    staff         721 Feb 12 03:01 http_server.py 
-rw-r--r--   1 7stud    staff         498 Jan 01 07:10 index.html 
-rw-r--r--   1 7stud    staff          68 Jan 01 05:03 oneliner.py 
-rw-r--r--   1 7stud    staff         954 Feb 11 09:05 socket_server.py 
-rw-r--r--   1 7stud    staff           0 Feb 15 02:50 test.png 
 
Name of file: test.png 
 
~/python_programs$ ls *.png 
bar_freq.png  example.png   test.png 
 
~/python_programs$