Giter VIP home page Giter VIP logo

ftptool's Introduction

ftptool

Higher-level ftplib

ftplib in itself is a bit raw, as it leaves details about the protocol for the user to handle. ftptool abstracts that away, and even provides a neat interface for file management.

Note

ftptool requires Python 2.5 or later.

Connecting & Authenticating

Code says more than words, so let's look at an example: connecting.

>>> a_host = FTPHost.connect("ftp.python.org", user="foo", password="bar")

connect is a classmethod that lets you create an FTPHost instance with an underlying ftplib.FTP instance.

Working with Directories

Changing Working Directory

Changing and getting the current directory is implemented as a property called current_directory. It is lazy; it won't ask the server which the current directory is until you ask for it.

Note that since it's a property, this will actually go one level up:

>>> a_host.current_directory = ".."

Similarly, this will descend into the "foo" directory:

>>> a_host.current_directory = "foo"

In most cases, it's easier to just specify absolute paths:

>>> a_host.current_directory = "/foo"

current_directory will always be the server-side representation; when you change directory, it ends up sending a CWD and then a PWD to get the result of the operation (since the FTP protocol doesn't define what the reply text to a CWD is.)

Listing and Walking the Directory Tree

A os.walk interface is implemented for walking the directory tree:

>>> for (dirname, subdirs, files) in a_host.walk("/a_dir"):
...     print dirname, "has file(s)", ", ".join(files)
...
/a_dir has file(s) foo, bar
/a_dir/other_dir has file(s) hello
/a_dir/some_dir has file(s)

Just like os.walk, you can remove entries in the subdirs list to avoid descending into them:

>>> for (dirname, subdirs, files) in a_host.walk("/a_dir"):
...     for subdir in subdirs:
...         if subdir.startswith("other_"):
...             subdirs.remove(subdir)
...     print dirname, "has file(s)", ", ".join(files)
...
/a_dir has file(s) foo, bar
/a_dir/some_dir has file(s)

You can non-recursively list a directory using listdir:

>>> a_host.listdir("/a_dir")
(['other_dir', 'some_dir'], ['foo', 'bar'])

Creating, Deleting and Renaming

The most simple form of creating a directory is mkdir. You simply give it a directory to create, and so it does:

>>> a_host.mkdir("/new_dir")

If you just want to ascertain that a directory is ready, i.e., exists for an upload, you could use makedirs which tries to create every part of the directory, piece by piece.

>>> a_host.makedirs("/a_dir/some_dir/a_new_dir/other_new_dir")

Would, hypothetically, create a_new_dir and other_new_dir.

ftptool implements it by first trying to change directory into the given path, to see if it exists, and then changes back. If it does, it simply returns, otherwise it creates the directories piece by piece.

Using the File Proxy

Files in ftptool are implemented using proxy objects called FTPFileProxy. They represent a file on a remote host. Using them is easy as pie!

>>> a_host.file_proxy("/a_dir/foo").download_to_str()
'This is the file "foo".'
>>> a_host.file_proxy("/a_dir/new_file").upload_from_str("Hello world!")

The Three Upload & Download Methods

ftptool provides three ways of uploading or downloading files: * to/from_str: using a str object, * to/from_file: using a filename, * and the default: using a file-like object.

Given:

>>> f = a_host.file_proxy("/foo.txt")

You could upload and download from str using these two:

>>> f.upload_from_str("Hi!")
>>> f.download_to_str()
'Hi!'

And using a filename like this:

>>> f.upload_from_file("/etc/motd")
>>> f.download_to_file("/tmp/motd")

And lastly, using file-like objects:

>>> f.upload(StringIO("Test!"))
>>> fp = StringIO()
>>> f.download(fp)
>>> fp.getvalue()
'Test!'

Renaming Files

Renaming is a method of the file proxies, called rename. It returns a new file proxy for the renamed-to file, so the common pattern will be:

>>> a_file = a_host.file_proxy("hello_world")
>>> a_file = a_file.rename("foobar")

This will issue a rename command, too, so a_file will essentially be the same as before, with a new name and a new instance ID.

Deleting Files

Deleting a file is much like renaming it: it's a method of the file proxies, called delete. It, however, doesn't have a meaningful return value.

>>> a_file.delete()

Mirroring

ftptool supports two types of mirroring: local to remote, and remote to local. As in, it can download a whole directory and all descendants into a local directory, for you to play with. It can also upload a whole directory to a remote host.

The first one, downloading, is called mirror_to_local. It's used like so:

>>> a_host.mirror_to_local('/a_dir', 'my_copy_of_a_dir')

The cousin, mirror_to_remote, has the same signature; source first, then destination.

>>> a_host.mirror_to_remote('my_copy_of_a_dir', '/a_dir')

If the local working directory is the one you want to upload, you can just give mirror_to_remote an empty string or a dot.

ftptool's People

Contributors

dbrandt avatar jbergstroem avatar lericson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ftptool's Issues

Add access testing methods

To make it easy to do some look-before-you-leap tests, a couple of new methods would be nice:

  • exists() - True/False if the path exists
  • is_writable() - True/False if the path is writable

These would be interesting, but it is uncertain how one would go about implementing it - FTP doesn't know how to check these things in a good way.

Especially checking writability will be an issue.

Listing doesnt handle total bytes line

I am seeing this error trying to walk an FTP server:

.local/share/virtualenvs/cds-etl-5bEvERRa/lib/python3.6/site-packages/ftptool.py", line 24, in _parse_list_line
raise ValueError("unknown line type %r" % line[:1])
ValueError: unknown line type 't'

The listing output of the directory is:

Listing: total 1023664
Listing: -rw-r--r-- 1 0 0 174088372 Aug 29 02:00 A16011.20180828.csv
Listing: -rw-r--r-- 1 0 0 174700852 Aug 30 02:00 A16011.20180829.csv
Listing: -rw-r--r-- 1 0 0 175297272 Aug 31 02:00 A16011.20180830.csv

I believe it should ignore a line starting with t or perhaps total

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.