Skip to main content

A Pathlib API for SFTP drive

Project description

sftppathlib

sftppathlib uses pathlib_abc and paramiko to create a pathlib API for SFTP clients.

The documentation is the same as the standard pathlib library, with some differences.

Differences

The methods expanduser(), readlink(), hardlink_to, replace(), owner(), group(), from_uri(), as_uri() are not supported. Subsequently, any method in pathlib_abc which relies on these will also fail. For some of them, it's because they don't have any meaning on SFTP clients, others because I don't trust myself to implement them correctly.

The methods stat(..., follow_symlinks), symlink_to(..., target_is_directory), chmod(..., follow_symlinks) have their named parameters ignored, since Paramiko's SFTPClient does not support them.

Some of Paramiko's SFTPClient methods would return status codes like SFTP_OK; these are ignored.

Usage

The sftppathlib relies on an instance of an SFTPClient to be used. This can be either be created in the background using the default setup, or manually and passing it to the __init__ constructor.

With setup

The default connection will use Paramiko's SSHClient to connect. If you are only connecting to one SFTP server, it's recommended to create a config file in the application directory:

  • Windows: ~/AppData/Roaming/sftppathlib/config.yaml
  • Linux: ~/.local/share/sftppathlib/config.yaml
  • Apple: ~/Library/Application Support/sftppathlib/config.yaml

The file should be the parameters passed to paramiko.SSHClient.connect.

hostname: sftp.<domain>
port: 22
username: <username>
password: <password>

It will also use the key defined in ~/.ssh/known_hosts. This will typically include an entry starting with [sftp.<domain>]:22.

After this, sftppathlib.SFTPPath can be used like pathlib.Path:

from sftppathlib import SFTPPath

root = SFTPPath("www/")
path = root / "hello.txt"

path.write_text("hello world", encoding="utf-8")
print(path.read_text(encoding="utf-8"))

for child in root.iterdir():
    print(child)

New 0.5.0: It is also possible to use the url by first calling set_authority. All urls will be passed through urllib.parse.urlparse, and the netloc (authority) attribute will be replaced with a prefix that is prepended to the path attribute.

from sftppathlib import SFTPPath

SFTPPath.set_authority("example.com", "www")

root = SFTPPath("example.com/")
path = root / "hello.txt"

path.write_text("hello world", encoding="utf-8")
print(path.read_text(encoding="utf-8"))

for child in root.iterdir():
    print(child)

Without setup

Note: The CREDENTIALS variable should be imported from another file or a module; never include secrets in code.

New 0.5.0: Added SFTPPath.from_config and config_credentials.

import paramiko
import sft
from sftppathlib import SFTPPath

SFTPPath.set_authority("example.com", "www")

CREDENTIALS = {
    "hostname": "sftp.<domain>",
    "port": 22,
    "username": "<username>",
    "password": "<password>",
}

# Alternative 1
root = SFTPPath.from_config("example.com/", CREDENTIALS)

# Alternative 2
config_credentials(CREDENTIALS)
root = SFTPPath("example.com")

...
pre 0.5
import paramiko
from sftppathlib import SFTPPath

CREDENTIALS = {
    "hostname": "sftp.<domain>",
    "port": 22,
    "username": "<username>",
    "password": "<password>",
}

ssh_client = paramiko.SSHClient()
ssh_client.load_system_host_keys()
ssh_client.connect(**CREDENTIALS)

sftp_client = paramiko.sftp_client.SFTPClient.from_transport(
    ssh_client.get_transport())


root = SFTPPath("www/", accessor=sftp_client)

...

If many instances are needed, it can be beneficial to subclass SFTPClient and create a Path method:

...

class SFTPPathClient(paramiko.sftp_client.SFTPClient):
    def Path(self, path, *paths):
        return SFTPPath(path, *paths, accessor=self)


sftp_path_client = SFTPPathClient.from_transport(
    ssh_client.get_transport())


root = sftp_path_client.SFTPPath("www/")

...

Closing connections

It is assumed that you want to keep the connections open during the duration of the program. If not, please use SFTPClient.close() and SSHClient.close().

Acknowledgments

Thanks to the paramiko/contributors and the pathlib-abc/contributors. This extends to anyone involved with the standard pathlib library, but I cannot find the list.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sftppathlib-0.5.1.tar.gz (16.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sftppathlib-0.5.1-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

Details for the file sftppathlib-0.5.1.tar.gz.

File metadata

  • Download URL: sftppathlib-0.5.1.tar.gz
  • Upload date:
  • Size: 16.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for sftppathlib-0.5.1.tar.gz
Algorithm Hash digest
SHA256 dcabced0b82d3782bfbc6aeca911bace70bc55ab2259942a80f3a1e542dae9ae
MD5 f017eb47aefbaa9582654c63c88f5fea
BLAKE2b-256 08753cdc5c9583e62f473f2676b9d4851be1ae78204dd22cfe4f1598a4c8e578

See more details on using hashes here.

File details

Details for the file sftppathlib-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: sftppathlib-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 17.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for sftppathlib-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cb075b2340afefa5910d8917dc178c2bbd3a80ad032fdad5faf9c506e69b53c7
MD5 78d10a4907b96c8aa01eb20e6a6fe12a
BLAKE2b-256 e22886dc95a0f711fc3ee3dda0c902806727591057718275d3f947028215770a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page