Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to read FTP files without writing them using Python?

Tags:

python

ftp

ftplib

I am trying to read files using Python's ftplib without writing them. Something roughly equivalent to:

def get_page(url):     try:         return urllib.urlopen(url).read()     except:         return "" 

but using FTP.

I tried:

def get_page(path):     try:         ftp = FTP('ftp.site.com', 'anonymous', 'passwd')         return ftp.retrbinary('RETR '+path, open('page').read())     except:         return '' 

but this doesn't work. The only examples in the docs involve writing files using the ftp.retrbinary('RETR README', open('README', 'wb').write) format. Is it possible to read ftp files without writing first?

like image 1000
aensm Avatar asked Jun 26 '12 13:06

aensm


People also ask

How do I open a FTP file in Python?

The ftplib module included in Python allows you to use Python scripts to quickly attach to an FTP server, locate files, and then download them to be processed locally. To open a connection to the FTP server, create an FTP server object using the ftplib. FTP([host [, user [, passwd]]]) method.

Can you FTP with Python?

FTP(File Transfer Protocol)For uploading and downloading the file, we will use ftplib Module in Python. It is an in-built module in Python.

How do I make a simple FTP server in Python?

Configuring and Starting an FTP Server First, SSH in to your server as root and install the Python pyftpdlib library. Next, log out of your server as root. The rest of your steps should be done while logged in as your app's system user. You can now start the FTP server.

What is the use of Python FTP?

Python - FTP - FTP or File Transfer Protocol is a well-known network protocol used to transfer files between computers in a network. It is created on client server architectu

Do you need to read or write a file in Python?

Whether it’s writing to a simple text file, reading a complicated server log, or even analyzing raw byte data, all of these situations require reading or writing a file. This tutorial is mainly for beginner to intermediate Pythonistas, but there are some tips in here that more advanced programmers may appreciate as well.

How to retrieve files hosted on FTP server in Python?

Most common web browsers can retrieve files hosted on FTP servers. In python we use the module ftplib which has the below required methods to list the files as we will transfer the files. Current working directory.

Is it possible to use it with FTP without authentication?

It is created on client server architecture and can be used along with user authentication. It can also be used without authentication but that will be less secure. FTP connection which maintains a current working directory and other flags, and each transfer requires a secondary connection through which the data is transferred.


1 Answers

Well, you have the answer right in front of you: The FTP.retrbinary method accepts as second parameter a reference to a function that is called whenever file content is retrieved from the FTP connection.

Here is a simple example:

#!/usr/bin/env python from ftplib import FTP  def writeFunc(s):   print "Read: " + s  ftp = FTP('ftp.kernel.org')  ftp.login() ftp.retrbinary('RETR /pub/README_ABOUT_BZ2_FILES', writeFunc) 

You should implement writeFunc so that it actually appends the data read to an internal variable, something like this, which uses a callable object:

#!/usr/bin/env python from ftplib import FTP  class Reader:   def __init__(self):     self.data = ""   def __call__(self,s):      self.data += s  ftp = FTP('ftp.kernel.org')  ftp.login() r = Reader() ftp.retrbinary('RETR /pub/README_ABOUT_BZ2_FILES', r)  print r.data 

Update: I realized that there is a module in the Python standard library that is meant for this kind of things, BytesIO:

#!/usr/bin/env python from ftplib import FTP from io import BytesIO  ftp = FTP('ftp.kernel.org')  ftp.login() r = BytesIO() ftp.retrbinary('RETR /pub/README_ABOUT_BZ2_FILES', r.write)  print r.getvalue() 
like image 79
daniel kullmann Avatar answered Sep 20 '22 00:09

daniel kullmann