Using Python FTP Library to Extract Files

This is my first post here, so I'm happy to be part of the community. I have a rather mundane question to ask, but it was a rather nasty problem, so I hope to find the answers.

So, I'm trying to use the Python FTPLIB module to extract a binary file.

The code entered directly into the interpreter is as follows:

>>> from ftplib import FTP 

>>> ftp = FTP('xxx.xx.xx.x')  # IP of target device

>>> ftp.login()

>>> file = "foobar.xyz" # target file 

>>> ftp.retrbinary("RETR " + file, open('filez.txt', 'wb').write)

While some functions work (I can view all files on the device, receive a welcome message from an FTP server application and even rename files), when I try to execute the last command above, I get

    error_perm                                Traceback (most recent call last)

/Users/user_name/<ipython console> in <module>()

/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ftplib.pyc in retrlines(self, cmd, callback)
    419         if callback is None: callback = print_line
    420         resp = self.sendcmd('TYPE A')
--> 421         conn = self.transfercmd(cmd)
    422         fp = conn.makefile('rb')
    423         while 1:

/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ftplib.pyc in transfercmd(self, cmd, rest)
    358     def transfercmd(self, cmd, rest=None):
    359         """Like ntransfercmd() but returns only the socket."""
--> 360         return self.ntransfercmd(cmd, rest)[0]
    361 
    362     def login(self, user = '', passwd = '', acct = ''):

/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ftplib.pyc in ntransfercmd(self, cmd, rest)
    327             if rest is not None:
    328                 self.sendcmd("REST %s" % rest)
--> 329             resp = self.sendcmd(cmd)
    330             # Some servers apparently send a 200 reply to

    331             # a LIST or STOR command, before the 150 reply


/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ftplib.pyc in sendcmd(self, cmd)
    241         '''Send a command and return the response.'''
    242         self.putcmd(cmd)
--> 243         return self.getresp()
    244 
    245     def voidcmd(self, cmd):

/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ftplib.pyc in getresp(self)
    216             raise error_temp, resp
    217         if c == '5':
--> 218             raise error_perm, resp
    219         raise error_proto, resp
    220 

error_perm: 502 Command not implemented.

ftplib, , Python FTP .

, - , . , , .

+3
3

, ( /, ftp.login()), .

ftp.login(user='foo', passwd='bar')

.

: ftplib (, ):

#!/usr/bin/env python

from ftplib import FTP

HOST   = "localhost"
UNAME  = "foo"
PASSWD = "bar"
DIR    = "pub"
FILE   = "test.test"

def download_cb(block):
    file.write(block)

ftp = FTP(HOST)
ftp.login(user=UNAME, passwd=PASSWD)
ftp.cwd(DIR)

file = open(FILE, "wb")
ftp.retrbinary("RETR " + FILE, download_cb)
file.close()
ftp.close()

. , retrbinary , ftp.sendcmd("TYPE I") . , ftp-.

+8

, ftplib modlue. urllib.urlretrieve , . :.

import urllib
urllib.urlretrieve('ftp://xxx.xx.xx.x/foobar.xyz', filename='filez.txt')

: urllib.request.urlretrieve python 3.x, ...

FTP- , url, (, ftp://user:password@server/path/to/file). , ftp , script, , , , .

+1

Works great for me, try with a different server (I tested the debian ftp server.):

ftp = FTP ('xx.xx.xx.xx') '

ftp.login ()

'230 Log in successfully.'

File = "robots.txt"

ftp.retrlines ("RETR" + file, open ('filez.txt', 'wb'). write)

'226 File send OK.

0
source

Source: https://habr.com/ru/post/1778521/


All Articles