2

I'm using pythons ftplib to try and retrieve files off a remote server (practice project). I've had no issues sending files but getting an error when trying to retrieve. I'm using python 3. This is my code:

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()

This is the error I get:

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 ?

Any idea what's causing the error?

Marlone
  • 79
  • 2
  • 8

1 Answers1

3

ftplib raises an error if the status code of the response starts with a '5'. That means the server is returning a 5xx error:

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.

According to the ftp docs:

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

But you wrote this:

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

which will produce something like this:

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

You need to add a space after 'RETR'.

Create your own ftp server with python

By the way, you can write a short python program that will act as an ftp server. First install the module pyftpdlib:

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

If you only have one version of python on your system, you can just write:

$ pip install pyftpdlib

If you have more than one version of python on your computer, substitute the correct version number for 3.4. The version number you specify is the python version where the pyftpdlib module will install.

Here is what an ftp server looks like in 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 docs and tutorial here. add_user() and permissions described here.

Then, you can create a directory for your server:

$ mkdir ftp_server
$ cd ftp_server
$ touch my_ftp_server.py

Put the code above into the file my_ftp_server.py. Then add some files to the ftp_server directory that you want to practice 'downloading'. Then start up the server by running my_ftp_server.py.

In another terminal window, switch directories to the directory containing your python program:

$ cd python_programs

Then make some minor changes to your ftp client program:

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()

Here's a sample run of your ftp client program:

~/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$ 
7stud
  • 46,922
  • 14
  • 101
  • 127
  • OK, that was it. Thanks. I really have to pay better attention to my syntax I guess. Always something simple. Thanks again! – Marlone Feb 15 '15 at 02:09
  • 1
    @Marlone, You're welcome! There is a better way to construct strings: ......................................................... `ftp_cmd = "RETR {}".format(filename)` then `ftp.retrbinary(ftp_cmd, localfile.write, 1024)`. See format() examples here: https://docs.python.org/3/library/string.html#format-examples. You could also use a python program for your ftp server. See how at the end of my post. – 7stud Feb 15 '15 at 07:12
  • 1
    Awesome, thanks for the additional info on creating an ftp server, guess that's my next project! – Marlone Feb 18 '15 at 01:04